Page #463 (34.3. Creating and Running Servlets)

 
[Page 1164 ( continued )]

34.4. The Servlet API

You have to know the servlet API in order to understand the source code in FirstServlet.java. The servlet API provides the interfaces and classes that support servlets. These interfaces and classes are grouped into two packages, javax.servlet and javax.servlet.http , as shown in Figure 34.6. The javax.servlet package provides basic interfaces, and the javax.servlet.http package provides classes and interfaces derived from them, which provide specific means for servicing HTTP requests .


[Page 1165]
Figure 34.6. The servlet API contains interfaces and classes that you use to develop and run servlets.


34.4.1. The Servlet Interface

The javax.servlet.Servlet interface defines the methods that all servlets must implement. The methods are listed below:

  /** Invoked for every servlet constructed */     public void   init()   throws   ServletException;   /** Invoked to respond to incoming requests */     public void   service(ServletRequest request, ServletResponse response)     throws   ServletException, IOException;   /** Invoked to release resource by the servlet */     public void   destroy();  

The init , service , and destroy methods are known as life-cycle methods and are called in the following sequence (see Figure 34.7):

  1. The init method is called when the servlet is first created, and is not called again as long as the servlet is not destroyed . This resembles an applet's init method, which is invoked after the applet is created, and is not invoked again as long as the applet is not destroyed.

  2. The service method is invoked each time the server receives a request for the servlet. The server spawns a new thread and invokes service .

  3. The destroy method is invoked after a timeout period has passed or the Web server is being terminated . This method releases resources for the servlet.

Figure 34.7. The JVM uses the init , service , and destroy methods to control the servlet.


[Page 1166]

34.4.2. The GenericServlet Class, ServletConfig Interface, and HttpServlet Class

The javax.servlet.GenericServlet class defines a generic, protocol-independent servlet. It implements javax.servlet.Servlet and javax.servlet.ServletConfig . ServletConfig is an interface that defines four methods ( getInitParameter , getInitParameterNames , getServletContext , and getServletName ) for obtaining information from a Web server during initialization. All the methods in Servlet and ServletConfig are implemented in GenericServlet except service . Therefore, GenericServlet is an abstract class.

The javax.servlet.http.HttpServlet class defines a servlet for the HTTP protocol. It extends GenericServlet and implements the service method. The service method is implemented as a dispatcher of HTTP requests. The HTTP requests are processed in the following methods:

  • doGet is invoked to respond to a GET request.

  • doPost is invoked to respond to a POST request.

  • doDelete is invoked to respond to a DELETE request. Such a request is normally used to delete a file on the server.

  • doPut is invoked to respond to a PUT request. Such a request is normally used to send a file to the server.

  • doOptions is invoked to respond to an OPTIONS request. This returns information about the server, such as which HTTP methods it supports.

  • doTrace is invoked to respond to a TRACE request. Such a request is normally used for debugging. This method returns an HTML page that contains appropriate trace information.

All these methods have the same signature:

   protected void   doXxx(HttpServletRequest req, HttpServletResponse resp)   throws   ServletException, java.io.IOException 

The HttpServlet class provides default implementation for these methods. You need to override doGet , doPost , doDelete , and doPut if you want the servlet to process a GET, POST, DELETE, or PUT request. By default, nothing will be done. Normally, you should not override the doOptions method unless the servlet implements new HTTP methods beyond those implemented by HTTP 1.1. Nor is there any need to override the doTrace method.

Note

GET and POST requests are often used, whereas DELETE, PUT, OPTIONS, and TRACE are not. For more information about these requests, please refer to the HTTP 1.1 specification from www.cis.ohio-state.edu/htbin/rfc/rfc2068.html.


Note

Although the methods in HttpServlet are all nonabstract, HttpServlet is defined as an abstract class. Thus you cannot create a servlet directly from HttpServlet . Instead you have to define your servlet by extending HttpServlet .


The relationship of these interfaces and classes is shown in Figure 34.8.

Figure 34.8. HttpServlet inherits abstract class GenericServlet , which implements interfaces Servlet and ServletConfig .
(This item is displayed on page 1167 in the print version)

34.4.3. The ServletRequest Interface and HttpServlet Request Interface

Every doXxx method in the HttpServlet class has a parameter of the HttpServletRequest type, which is an object that contains HTTP request information, including parameter name and values, attributes, and an input stream. HttpServletRequest is a subinterface of ServletRequest . ServletRequest defines a more general interface to provide information for all kinds of clients . The frequently used methods in these two interfaces are shown in Figure 34.9.


[Page 1167]
Figure 34.9. HttpServletRequest is a subinterface of ServletRequest .


[Page 1168]

34.4.4. The ServletResponse Interface and HttpServletResponse Interface

Every do Xxx method in the HttpServlet class has a parameter of the HttpServletResponse type, which is an object that assists a servlet in sending a response to the client. HttpServletResponse is a subinterface of ServletResponse . ServletResponse defines a more general interface for sending output to the client.

The frequently used methods in these two interfaces are shown in Figure 34.10.

Figure 34.10. HttpServletResponse is a subinterface of ServletResponse .

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net