Java Servlets - PowerPoint PPT Presentation

About This Presentation
Title:

Java Servlets

Description:

Invoke it via new CGI shell process. Right way to use Java as CGI ... http://www.yahoo.com/search?music. Here, the text field contains music. ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 11
Provided by: gu8
Category:
Tags: java | music | servlets | yahoo

less

Transcript and Presenter's Notes

Title: Java Servlets


1
Java Servlets
2
Basics
  • What is Java Servlets?
  • Javas solution to CGI programs.
  • Wrong way to use Java as CGI
  • Write stand-alone Java program
  • Invoke it via new CGI shell process.
  • Right way to use Java as CGI
  • Use servlets and a servlet-compatible server.

3
How it works?
  • Browser makes a servlet request to webserver.
  • Webserver recognizes the URL as a servlet URL.
    (e.g., http//www.obvious.com/servlets/blah)
  • Webserver loads servlet if not already loaded.
  • Servlet executes and delivers output to webserver
    (HTML output).
  • Webserver delivers output to Browser.

4
What you need to write servlets?
  • Either JDK 1.1 and JSDK (Java Servlet Development
    Kit).
  • JDK 1.2
  • A servlet-compatible webserver.

5
GET POST
  • When Form data is sent by the browser, it can be
    sent in one of two ways (1) using the GET method
    and (2) using the POST method.
  • In the GET method, the form data (parameters) is
    appended to the URL, as in
  • http//www.yahoo.com/search?music
  • Here, the text field contains music.
  • In the POST method, the browser simply sends the
    form data directly.
  • When you create an HTML form, you decide whether
    you want to use GET or POST.
  • When you use GET, the doGet() method of your
    servlet is called, otherwise the doPost() method
    is called.
  • The standard practice is to use POST, unless you
    need to use GET.
  • You need to use GET when you want a link to
    invoke a CGI program (servlet).

6
Your first servlet -- Hello World
  • import javax.servlet.
  • import javax.servlet.http.
  • import java.io.
  • public class helloworld extends HttpServlet
  • public void doGet (HttpServletRequest req,
    HttpServletResponse resp) throws
    ServletException, IOException
  • // Set the content type of the response.
  • resp.setContentType ("text/html")
  • // Create a PrintWriter to write the response.
  • java.io.PrintWriter out new PrintWriter
    (resp.getOutputStream())
  • // The first part of the response.
  • out.println ("") out.println ("") out.println
    ("")
  • // The greeting.
  • out.println ("Yo, Hello World!")
  • // Last part. out.println ("") out.println
    ("") out.close()
  • // Screen I/O
  • System.out.println ("Inside servlet ... servlet
    complete")
  • public void doPost (HttpServletRequest req,
    HttpServletResponse resp) throws
    ServletException, IOException doGet (req,
    resp)

7
Hello World Notes
  • The servlet class libraries need to be imported,
    along with java.io.PrintWriter.
  • There are two methods, one for each type of
    request GET or POST.
  • You can decide not to "really" implement one
    method and instead have it call the other.
  • There are two object parameters to each method,
    the "request" and the "respponse".
  • The HttpServletResponse instance has an
    OutputStream that is used to write directly to
    the requesting browser.
  • To make writing easier, we wrap a PrintWriter
    around the OutputStream instance
  • // Create a PrintWriter to write the
    response.
  • java.io.PrintWriter out new
    PrintWriter (resp.getOutputStream())
  • We set the content-type (as required by the HTTP
    protocol)
  • // Set the content type of the
    response.
  • resp.setContentType ("text/html")
  • We write HTML to the output, e.g.,
  • out.println ("")
  • out.println ("")
  • Don't forget to close the output stream
  • out.close()
  • Optionally, for debugging, we can also write to
    the local screen where the webserver is running
  • System.out.println ("Inside servlet ...
    servlet complete")

8
Extracting Parameters in a Servlet
  • Idea -- create an HTML Form, and have a servlet
    pick up the Form data entered by the user.
  • HTML Form
  • lthtmlgt
  • ltheadgtlttitlegtTest Postlt/titlegtlt/headgt
  • ltbodygt
  • ltform action"http//localhost8502/servlets/Tes
    tform" method"post"gt Enter a string ltinput
    type"text" name"param1"gt And then press "Go"
    ltinput type"submit" value"Go"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

9
Extracting Parameters in a Servlet
  • Next the Java Servlet
  • import javax.servlet.
  • import javax.servlet.http.
  • import java.io.
  • import java.util.
  • public class TestForm extends HttpServlet
  • public void doPost (HttpServletRequest req,
    HttpServletResponse resp) throws
    ServletException, IOException
  • // Set the content type of the response.
  • resp.setContentType ("text/html")
  • // Create a PrintWriter to write the response.
  • java.io.PrintWriter out new PrintWriter
    (resp.getOutputStream()) // The first part of
    the response.
  • out.println ("") out.println ("") out.println
    ("")
  • // Now get the parameters and output them back.
  • out.println ("Request parameters ")
  • Enumeration e req.getParameterNames()
  • while (e.hasMoreElements())
  • String name (String) e.nextElement()
  • String value req.getParameter (name)
  • if (value ! null) out.println (" name"
    name " value" value "")

10
Notes for Extracting Parameters
  • We need to import java.util.Enumeration. Whatever
    parameters were provided are all in the
    HttpServletRequest instance. We can list these
    parameters by getting an Enumeration instance
    from the request by calling getParameterNames()
  • Enumeration e req.getParameterNames()
  • The "name" of a parameter is really the string in
    the name attribute of the tag for the particular
    Form element. For example, the name string is
    param1 below.
  • Enter a string ltinput type"text"
    name"param1"gt
  • Now, if we want to retrieve the actual string
    typed in by the user, we use that name in
    getParameter()
  • String whatTheUserTyped
    req.getParameter
  • ("param1")
Write a Comment
User Comments (0)
About PowerShow.com