javax.servlet.http
Class HttpServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
All Implemented Interfaces:
java.io.Serializable, Servlet, ServletConfig
Direct Known Subclasses:
AdminRestServlet, DirectoryServlet, HessianServlet, HmtpServlet, JMSServlet, ProfilerServlet, QuercusServlet, QuercusServletImpl, SSIServlet

public abstract class HttpServlet
extends GenericServlet
implements java.io.Serializable

HttpServlet is a convenient abstract class for creating servlets. Normally, servlet writers will only need to override doGet or doPost.

Caching

HttpServlet makes caching simple. Just override getLastModified. As long as the page hasn't changed, it can avoid the overhead of any heavy processing or database queries. You cannot use getLastModified if the response depends on sessions, cookies, or any headers in the servlet request.

Hello, world

The Hello.java belongs in myapp/WEB-INF/classes/test/Hello.java under the application's root. Normally, it will be called as http://myhost.com/myapp/servlet/test.Hello. If the server doesn't use applications, then use /servlet/test.Hello.
 package test;

 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;

 public class Hello extends HttpServlet {
   public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
     throws ServletException, IOException
   {
     response.setContentType("text/html");
     PrintWriter out = response.getWriter();
     out.println("Hello, World");
     out.close();
   }
 }
 

See Also:
Serialized Form

Constructor Summary
HttpServlet()
           
 
Method Summary
protected  void doDelete(HttpServletRequest req, HttpServletResponse res)
          Process a DELETE request
protected  void doGet(HttpServletRequest req, HttpServletResponse res)
          Process a GET or HEAD request
protected  void doHead(HttpServletRequest req, HttpServletResponse res)
          Process a HEAD request.
protected  void doOptions(HttpServletRequest req, HttpServletResponse res)
          Process an OPTIONS request
protected  void doPost(HttpServletRequest req, HttpServletResponse res)
          Process a POST request
protected  void doPut(HttpServletRequest req, HttpServletResponse res)
          Process a PUT request
protected  void doTrace(HttpServletRequest req, HttpServletResponse res)
          Process a TRACE request
protected  long getLastModified(HttpServletRequest req)
          Returns the last-modified time for the page for caching.
protected  void service(HttpServletRequest req, HttpServletResponse res)
          Services a HTTP request.
 void service(ServletRequest request, ServletResponse response)
          Service a request.
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HttpServlet

public HttpServlet()
Method Detail

service

public void service(ServletRequest request,
                    ServletResponse response)
             throws ServletException,
                    java.io.IOException
Service a request. Normally not overridden. If you need to override this, use GenericServlet instead.

Specified by:
service in interface Servlet
Parameters:
request - request information. Normally servlets will cast this to HttpServletRequest
response - response information. Normally servlets will cast this to HttpServletRequest
Throws:
ServletException
java.io.IOException

service

protected void service(HttpServletRequest req,
                       HttpServletResponse res)
                throws ServletException,
                       java.io.IOException
Services a HTTP request. Automatically dispatches based on the request method and handles "If-Modified-Since" headers. Normally not overridden.

Parameters:
req - request information
res - response object for returning data to the client.
Throws:
ServletException
java.io.IOException

getLastModified

protected long getLastModified(HttpServletRequest req)
Returns the last-modified time for the page for caching. If at all possible, pages should override getLastModified to improve performance. Servlet engines like Resin can cache the results of the page, resulting in near-static performance.

Parameters:
req - the request
Returns:
the last-modified time of the page.

doHead

protected void doHead(HttpServletRequest req,
                      HttpServletResponse res)
               throws ServletException,
                      java.io.IOException
Process a HEAD request. By default, uses doGet.

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doGet

protected void doGet(HttpServletRequest req,
                     HttpServletResponse res)
              throws ServletException,
                     java.io.IOException
Process a GET or HEAD request

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doPost

protected void doPost(HttpServletRequest req,
                      HttpServletResponse res)
               throws ServletException,
                      java.io.IOException
Process a POST request

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doPut

protected void doPut(HttpServletRequest req,
                     HttpServletResponse res)
              throws ServletException,
                     java.io.IOException
Process a PUT request

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doDelete

protected void doDelete(HttpServletRequest req,
                        HttpServletResponse res)
                 throws ServletException,
                        java.io.IOException
Process a DELETE request

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doOptions

protected void doOptions(HttpServletRequest req,
                         HttpServletResponse res)
                  throws ServletException,
                         java.io.IOException
Process an OPTIONS request

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doTrace

protected void doTrace(HttpServletRequest req,
                       HttpServletResponse res)
                throws ServletException,
                       java.io.IOException
Process a TRACE request

Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException