Servlet methods - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Servlet methods

Description:

Servlet methods. Some methods inherited from the HttpServlet class: init ... response) throws IOException, ServletException ... ( IOException ioe ) ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 10
Provided by: julie387
Category:

less

Transcript and Presenter's Notes

Title: Servlet methods


1
Servlet methods
  • Some methods inherited from the HttpServlet
    class
  • init ? initialization code here
  • doGet ? handle a get request here
  • doPost ? handle a post request here
  • destroy ? cleanup code here
  • These methods can be overridden by you, the
    programmer
  • These methods are called automatically
  • ? Remember applets ?

2
Servlet doGet method
  • Called by the server to allow a servlet to handle
    a GET request.
  • When overriding this method,
  • read the request data,
  • get the response's writer or output stream
    object,
  • write the response headers,
  • write the response data.
  • When using a PrintWriter object to return the
    response, set the content type before accessing
    the PrintWriter object.

3
Servlet doGet method
  • The method header is
  • public void doGet( HttpServletRequest request,
  • HttpServletResponse response)
  • throws IOException,

  • ServletException
  • ? We have access to the HttpServletRequest and
    HttpServletResponse parameters (remember the 2
    implicit objects request and response in JSPs)

4
Perl, PHP, JSPs, Servlets
5
Servlet doGet method
  • // Inside doGet
  • // set the content type
  • response.setContentType( text/html )
  • / get a PrintWriter object so that we can output
    content /
  • PrintWriter out response.getWriter( )
  • // Output contents
  • out.println( lthtmlgt .. lt/htmlgt )

6
First Servlet
  • package helloWorld
  • import javax.servlet.
  • import javax.servlet.http.
  • import java.io.

7
First Servlet
  • public class HWServlet extends HttpServlet
  • public void doGet( HttpServletRequest request,
  • HttpServletResponse response) throws
    IOException, ServletException
  • response.setContentType( text/html )
  • try
  • PrintWriter out response.getWriter( )
  • out.println( lthtmlgtltbodygtHello
    Worldlt/bodygtlt/htmlgt )
  • catch( IOException ioe )

8
First Servlet
  • Place HelloServlet.class in webapps directory in
    a package
  • Map it to helloFromHF (or something else) in
    web.xml
  • url is http//leda.capitol-college.edu8180/hfran
    ceschi/helloFromHF

9
Servlet Mapping
  • Inside web.xml
  • ltservletgt
  • ltservlet-namegtHelloServletlt/servlet-namegt
  • ltservlet-classgtmypackage.HelloServletlt/ser
    vlet-classgt
  • lt/servletgt
  • ltservlet-mappinggt
  • ltservlet-namegtHelloServletlt/servlet-namegt
  • lturl-patterngt/helloFromHFlt/url-patterngt
  • lt/servlet-mappinggt
Write a Comment
User Comments (0)
About PowerShow.com