Java Servlets - PowerPoint PPT Presentation

About This Presentation
Title:

Java Servlets

Description:

Stored and executed on the server. Additional 'helper' classes ... throws ServletException, IOException { ServletOutputStream out = res.getOutputStream ... – PowerPoint PPT presentation

Number of Views:291
Avg rating:3.0/5.0
Slides: 33
Provided by: joseph98
Category:

less

Transcript and Presenter's Notes

Title: Java Servlets


1
Java Servlets
  • Joe Komar

2
Servlets Overview
  • Java programs
  • Stored and executed on the server
  • Additional helper classes
  • Part of javax.servlet java.servlet.http API
  • Memory resident
  • Server needs to support it
  • Can have persistent connection to a database
  • Can do servlet chaining
  • SDK from Sun needed

3
A Simple Servlet - HTML
lthtmlgt ltheadgtltTitlegtKomar's first
servletlt/titlegtlt/headgt ltbodygt ltform
actionhttp//140.209.124.1078080/servlet/JAK1
methodPOSTgt Enter your name ltinput typetext
namenamegtltbrgtltbrgt Enter your favorite movie
ltinput typetext namemoviegtltbrgt ltbrgtThanks! ltinpu
t typesubmitgt lt/formgt lt/bodygt lt/htmlgt
4
A Simple Servlet - Browser Screen
5
A Simple Servlet - The Code
import java.io. import javax.servlet. import
javax.servlet.http.
6
A Simple Servlet - The Code
public class JAK_Servlet1 extends HttpServlet
public void doPost (HttpServletRequest req,
HttpServletResponse res)
throws ServletException,
IOException ServletOutputStream out
res.getOutputStream() res.setContentType(
"text/html") String name
req.getParameter("name") String movie
req.getParameter("movie")
out.println("lthtmlgtltheadgtlttitlegtMovieslt/titlegtlt/he
adgt") out.println("ltbodygtlth1gtWhat you
said....lt/h1gtlthrgt") out.println("ltpgtName
" name) out.println("ltpgtMovie "
movie) out.println("lt/bodygtlt/htmlgt")
out.close()
7
A Simple Servlet -- Output
8
Servlet Development at UST
  • Create the Form (or Java Applet)
  • Action POST 140.209.124.1078080/servlet/servlet
    name
  • FTP it to the server
  • IP address 140.209.124.107
  • Username -- class (case sensitive)
  • Password -- skeeter (case sensitive)
  • Change to public_html directory, then to your
    directory (three initials)
  • Put HTML file there

9
Servlet Development at UST
  • Check out your form -- 140.209.124.1078080/initia
    ls/pagename.html
  • Write your servlet (put package initials as the
    first line) and compile it
  • Move the .class file to the server
  • Same IP, username, and password
  • Change to servlets directory, then your
    subdirectory (initials) and put it there

10
Servlet Development at UST
  • Register your servlet on the server
  • From browser, enter address 140.209.124.1079090
  • Login as user admin, password testitout
  • Make sure Web Service is highlighted then click
    on Manage
  • Click on Servlets button on top of page
  • Click on Add choice in list

11
Servlet Development at UST
  • Registering your servlet (continued)
  • The Name field is the name you use in your
    forms Action attribute -- e.g., JAK1
  • The class is the name of the class file you
    transferred with your initials, period prepended
    and without the .class extension -- e.g.,
    jak.JoeOne
  • Click on the Add button
  • Enter whatever you want in the description
  • Click on Save
  • Close the Web Service window
  • Click on the Log Out button

12
Servlet Development at UST
  • Check out your Form/servlet application
  • Hints
  • Make sure you type things like the name of the
    class correctly on the server
  • Keep the FTP session open when you check things
    out -- will probably have to make changes and FTP
    things again
  • Make sure you hit the Reload or Refresh buttons
    after FTP changes
  • Dont go changing other server settings!!

13
Servlet Assignment
  • Create an HTML form that has the following
  • Text box for Operator ( - /) (select box??)
  • Two text boxes for operands
  • Submit button
  • Action is a POST to a named servlet
  • Write the Java Servlet
  • Takes the operation and operand, performs the
    requested operation
  • Creates a web page to show the inputs and results

14
Servlet JDBC Example - HTML
lthtmlgt ltheadgtltTitlegtKomar's Directory
Servletlt/titlegtlt/headgt ltbodygt ltform
actionhttp//140.209.124.1078080/servlet/JAK3
methodPOSTgt Enter the name or a portion thereof
ltinput typetext namenamegtltbrgtltbrgt ltbrgtThanks! lti
nput typesubmitgt lt/formgt lt/bodygt lt/htmlgt
15
Servlet JDBC Example - Browser
16
Servlet JDBC Example - Servlet
package jak import java.io. import
javax.servlet. import javax.servlet.http. impo
rt java.sql.
17
Servlet JDBC Example - Servlet
public class JAK_Directory extends HttpServlet
public void doPost (HttpServletRequest req,
HttpServletResponse res)
throws ServletException,
IOException ServletOutputStream out
res.getOutputStream() res.setContentType("
text/html") String name
req.getParameter("name")
18
Servlet JDBC Example - Servlet
String url "jdbcodbcDirectory" String query
"select from STUDIR where Name like "
"'" name "'" Statement
stmt Connection con try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") c
atch (java.lang.ClassNotFoundException e)
System.err.print("Driver class not found")
19
Servlet JDBC Example - Servlet
try con DriverManager.getConnection(url)
stmt con.createStatement() ResultSet
rs stmt.executeQuery(query)
out.println("lthtmlgtltheadgtlttitlegtQuery
Resultlt/titlegtlt/headgt") out.println("ltcentergt
lth1gtResults of query for " "'" name
"'lt/h1gtlt/centergt") out.println("lthrgt")
20
Servlet JDBC Example - Servlet
while (rs.next()) out.println("ltpgtNam
e " rs.getString(1))
out.println("ltbrgtUsername " rs.getString(2))
out.println(" College Code "
rs.getString(3)) stmt.close()
con.close() catch(Exception ex)
System.err.print("SQL Exception "
ex.getMessage())
21
Servlet JDBC Example - Output
22
Core Servlet Interfaces and Class
  • Servlet interface
  • Methods all servlets must implement
  • Typically extend GenericServlet or HttpServlet
    classes
  • Server initializes the servlet with the init()
    method, asks for services with the service()
    method, destroys a it with the destroy() method
  • getServletConfig() and getServletInfo()

23
Core Servlet Interfaces and Class
  • ServletConfig interface
  • access to configuration data
  • getInitParameter(name) -- value of parameter
  • getInitParamenterNames() -- Enumeration of
    parameter names
  • getServerContext() -- returns a ServletContext
    object

24
Core Servlet Interfaces and Class
  • ServletContext interface
  • access to information about the environment
  • getAttribute(name) -- value of the named
    attribute of the network service
  • getMimeType(file) -- MIME type of the specified
    file
  • getRealPath(virtual-path) -- returns real path
  • getServerInfo() -- name version of server
  • getServlet(name) -- servlet named
  • getServlets() -- Enumeration of Servlets objects

25
Core Servlet Interfaces and Class
  • GenericServlet class
  • implements Servlet and ServletConfig interfaces
  • provides init(), destroy(), ServletConfig
    methods, and log() method
  • must override and implement the service() method

26
Core Servlet Interfaces and Class
  • ServletRequest interface
  • access data passed from the client to the servlet
  • contains parameter names, parameter values,
    attributes, and a ServletInputStream
  • getInputStream() -- returns a ServletInputStream
    object for reading the request body
  • many other methods to get information about the
    request and its context

27
Core Servlet Interfaces and Class
  • ServletResponse interface
  • to return response data to client
  • getOutputStream() -- returns an output stream to
    write to
  • setContentLength() -- sets content length for the
    response
  • setContentType() -- sets the content type for the
    response

28
Core Servlet Interfaces and Class
  • ServletInputStream class
  • readLine() -- to read into an array of bytes
  • ServletOutputStream class
  • overloaded print() and println() methods available

29
HTTP Servlet Interfaces and Classes
  • HttpServlet abstract class
  • extends GenericServlet
  • your class extends this one and override at least
    one method
  • doPost() and doGet() most commonly overridden
  • to support HTTP 1.1 commands (OPTION, PUT,
    DELETE, TRACE), override the service() method

30
HTTP Servlet Interfaces and Classes
  • HttpServletRequest interface
  • inherits from ServletRequest
  • methods to extract HTTP header information
  • getDateHeader(), getHeaderNames(), getPathInfo(),
    getRemoteUser(), etc.
  • getParameter(name) -- returns the String value of
    the named parameter

31
HTTP Servlet Interfaces and Classes
  • HttpServletResponse interface
  • inherits from ServletResponse
  • variables for error standard error conditions
  • most of the response header information is
    standard and need not change

32
HTTP Servlet Interfaces and Classes
  • HttpUtils class
  • collection of HTTP utility methods
  • getRequestURL() -- URL of the HttpServletRequest
    object
  • parsePostData() -- returns POSTs form data in a
    Hashtable
  • parseQueryString() -- returns a Hashtable of
    name/value pairs
Write a Comment
User Comments (0)
About PowerShow.com