HttpServlet

Java Servlet Programming, 2nd Edition > B. HTTP Servlet API Quick Reference > HttpServlet

 
< BACKCONTINUE >
HttpServlet

Synopsis

Class Name: javax.servlet.http.HttpServlet

Superclass: javax.servlet.GenericServlet

Immediate Subclasses: None

Interfaces Implemented: javax.servlet.Servlet, java.io.Serializable

Availability: Servlet API 1.0 and later

Description

HttpServlet is an abstract class that serves as the base class for HTTP (World Wide Web) servlets. The public service( ) method dispatches requests to an HTTP-specific, protected service( ) method, which then dispatches requests to particular handler functions for each HTTP submission type: doGet( ), doPost( ), and so on. Because the default HTTP servlet implementation handles dispatching to these methods, if you override the protected service( ) method, you must either handle the dispatching manually or not use the handler functions for HTTP request methods.

Class Summary

public abstract class HttpServlet extends javax.servlet.GenericServlet   implements javax.servlet.Servlet, java.io.Serializable {   // Constructors   public HttpServlet();   // Public instance method   public void service(ServletRequest req, ServletResponse res)     throws ServletException, IOException;   // Protected instance methods   protected void doDelete(HttpServletRequest req, HttpServletResponse res)     throws ServletException, IOException;                   // New in 2.0   protected void doGet(HttpServletRequest req, HttpServletResponse res)     throws ServletException, IOException;   protected void doOptions(HttpServletRequest req, HttpServletResponse res)     throws ServletException, IOException;                   // New in 2.0   protected void doPost(HttpServletRequest req,  HttpServletResponse res)     throws ServletException, IOException;   protected void doPut(HttpServletRequest req, HttpServletResponse res)     throws ServletException, IOException;                   // New in 2.0   protected void doTrace(HttpServletRequest req, HttpServletResponse res)     throws ServletException, IOException;                   // New in 2.0   protected long getLastModified(HttpServletRequest req);   protected void service(HttpServletRequest req, HttpServletResponse res)     throws ServletException, IOException; }

Constructors

HttpServlet()

public HttpServlet()
Description

The default constructor does nothing. Because you cannot be sure of how and when classes will be loaded, it is not advisable to override this constructor to perform startup tasks. Use init( ) instead.

Public Instance Methods

service()

public void service(ServletRequest req, ServletResponse res)   throws ServletException, IOException
Description

This service( ) method handles dispatching requests to the protected, HTTP-specific service( ) method; it should generally not be overridden.

Protected Instance Methods

doDelete()

protected void doDelete(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The default service( ) implementation in HttpServlet dispatches all HTTP DELETE requests to this method. Servlets implement this method to handle DELETE requests. The default implementation returns an HTTP SC_BAD_REQUEST error. This method was introduced in the Servlet API 2.0.

doGet()

protected void doGet(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The default service( ) implementation in HttpServlet dispatches all HTTP GET requests to this method. Servlets implement this method to handle GET requests. The default implementation returns an HTTP SC_BAD_REQUEST error.

doOptions()

protected void doOptions(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The default service( ) implementation in HttpServlet dispatches all HTTP OPTIONS requests to this method. The default implementation determines which options are supported and returns an appropriate header. For example, if a servlet overrides doGet( ) and doPost( ), the browser is informed that GET, POST, HEAD, TRACE, and OPTIONS are supported. There is almost never any reason to override this method. This method was introduced in Servlet API 2.0.

doPost()

protected void doPost(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The default service( ) implementation in HttpServlet dispatches all HTTP POST requests to this method. Servlets implement this method to handle POST requests. The default implementation returns an HTTP SC_BAD_REQUEST error.

doPut()

protected void doPut(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The default service( ) implementation in HttpServlet dispatches all HTTP PUT requests to this method. Servlets implement this to handle PUT requests. The default implementation returns an HTTP SC_BAD_REQUEST error. See RFC 2068 at http://www.ietf.org/rfc/rfc2068.txt for more on HTTP PUT requests. This method was introduced in Servlet API 2.0.

doTrace()

protected void doTrace(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The default service( ) implementation in HttpServlet dispatches all HTTP TRACE requests to this method. The default implementation returns a message listing all of the headers sent in the TRACE request. There is almost never any reason to override this method. This method was introduced in Servlet API 2.0.

getLastModified()

protected long getLastModified(HttpServletRequest req)
Description

Returns the date and time (expressed as milliseconds since midnight, January 1, 1970, GMT) that the content produced by the servlet was last modified. Negative values indicate that the time is not known. The default implementation returns -1. Called by servers in support of conditional HTTP GET requests and to manage response caches. See Chapter 4 for more information.

service()

protected void service(HttpServletRequest req, HttpServletResponse res)   throws ServletException, IOException
Description

The public service( ) method dispatches requests to this service( ) method. The method handles dispatching requests to doGet( ), doPost( ), and the other handler functions based on the type of request. If this method is overridden, no handlers are called.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

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