Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javax.servlet.http
Class HttpServlet

    java.lang.Object
      |
      +--javax.servlet.GenericServlet
            |
            +--javax.servlet.http.HttpServlet

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
       {
         PrintWriter out;

         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` | `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`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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.
  Tags copied from interface: Servlet
  Parameters:
    `req` - request information. Normally servlets will cast this to `HttpServletRequest`
    `res` - response information. Normally servlets will cast this to `HttpServletRequest`

### 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.

### 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.

### 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

### 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

### 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

### 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

### 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

### 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

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD