| Previous | Table of Contents | Next |
Although GenericServlet provides a good base for writing a servlet, it does not provide convenient access to HTTP-specific information. In particular, generic servlets do not distinguish among the varieties of request types provided by the HTTP specification. HTTP also defines specific headers for requests and has been extended to support concepts like cookies.
Rather than require all servlets to work via the Web protocol HTTP, the servlet libraries allow programmers to choose their servlet implementation. In the previous examples, we used GenericServlet as the parent class. This allowed access to HTTP style requests, but it also allows other forms of requests such as RMI or CORBA. For servlets intended to handle only HTTP requests, the javax.servlet.http.HttpServlet class specializes GenericServlet.
Perhaps the most telling of the methods added by HttpServlet, as listed in Table 7.9, are the ones used to deal with specific request types. For example, doGet is providedto handle HTTP GET requests; doPost is provided for HTTP POST requests. The second important specialization provided by HttpServlet is the creation of special request and response objects that provide access to HTTP-specific information.
| Table 7.8 GenericServlet Methods | |
| METHOD | DESCRIPTION |
|---|---|
| public void destroy() | Logs the destruction in the servlet log file. |
| public String getInitParameter(String) | Returns a string containing the value of the named initialization parameter or null if the requested parameter does exist. |
| public Enumeration getInitParameterNames() | Returns the names of the servlets initialization parameters as an enumeration of strings or an empty enumeration if there are no initialization parameters. |
| public ServletConfig getServletConfig() | Returns a servletConfig object containing any startup configuration information for this servlet. This object was set in the init method. |
| public ServletContext getServletContext() | Returns a ServletContext object, which contains information about the network service in which the servlet is running. The context is acquired from the configuration. |
| public String getServletInfo() | Returns a string that contains information about the servlet, such as its author, version, and copyright. This method should be overridden appropriately by subclasses. For example, you might return the string Search Servlet, Stephen & Scott, version 1.0 1998. |
| public void init(ServletConfig) | Initializes the servlet by storing the config object in an instance variable and logs the initialization. |
| public void log(String) | Writes the class name of the servlet and the given message to the servlet log file. |
| public void service(ServletRequest, ServletResponse) throws ServletException, IOException | Implements the service method by doing nothing. This method is intended to be overridden. |
| Table 7.9 HttpServlet Methods | |
| METHOD | DESCRIPTION |
|---|---|
| void doDelete(HttpServletRequest, HttpServletResponse) | Performs the HTTP DELETE operation; the default implementation reports an HTTP BAD_REQUEST error to the client, indicating that DELETE operations are not supported. |
| void doGet(HttpServletRequest, HttpServletResponse) | Performs the HTTP GET operation; the default implementation reports an HTTP BAD_REQUEST error to the client, indicating that GET operations are not supported. |
| void doOptions(HttpServletRequest, HttpServletResponse) | Performs the HTTP OPTIONS operation; the default implementation of this method automatically determines the HTTP Options that are supported by checking the Java run time for the methods defined in the servlets class. If a subclass overrides one of the doXXX methods, that request option will automatically show up in doOptions. |
| void doPost(HttpServletRequest, HttpServletResponse) | Performs the HTTP POST operation; the default implementation reports an HTTP BAD_REQUEST error to the client, indicating that POST operations are not supported. |
| void doPut(HttpServletRequest, HttpServletResponse) | Performs the HTTP PUT operation; the default implementation reports an HTTP BAD_REQUEST error to the client, indicating that PUT operations are not supported. |
| void doTrace(HttpServletRequest, HttpServletResponse) | Performs the HTTP TRACE operation; the default implementation of this method causes a response with a message containing all of the headers sent in the trace request. Servlets will rarely override this method. |
| long getLastModified( HttpServletRequest) | Gets the time the requested entity was last modified. This time is returned in a long integer indicating milliseconds since midnight, January 1, 1970, UTC. The default implementation returns a negative number, indicating that the modification time is unknown. In this case, the modification time should not be used for GET or other cache operations. |
| void service(HttpServletRequest, HttpServletResponse) | Checks the request method and calls the appropriate request handler. For example, POST requests result in a call to doPost. If the method is unknown, an error message is sent to the client. |
| void service(ServletRequest, ServletResponse) | Checks whether this is an HTTP request. If not, an exception is thrown. If this is an HTTP request, the request and response objects are cast appropriately, and the previously defined service method is called. |
Notice that many of these methods report errors to the client if a particular request type is not supported. These errors are defined and managed in the HttpServletResponse object, as described in the upcoming section, HttpServletResponse. To support HTTP style servlets, the javax.servlet.http library also provides the class HttpUtils. This class provides three methods, as listed in Table 7.10, for managing HTTP-specific data.
For the most part, these methods are used by the library and will probably not be accessed directly by servlets.
| Previous | Table of Contents | Next |