HttpServlet Class |
public abstract class HttpServlet extends GenericServlet implements Serializable |
    Object
       GenericServlet
        HttpServlet
Interfaces Serializable The HttpServlet class extends the GenericServlet class to provide functionality tailored to the HTTP protocol. It provides methods for handling HTTP DELETE, GET, OPTIONS, POST, PUT, and TRACE requests. Like the GenericServlet class, the HttpServlet class provides a service() method, but unlike the GenericServlet class the service() method is rarely overridden with HttpServlets. The default implementation of the service() method dispatches the request to the appropriate handler method. A concrete sub-class of HttpServlet must override at least one of the methods defined in the HttpServlet or GenericServlet classes. The doDelete(), doGet(), doPost(), or doPut() methods are the ones most commonly overridden. Example: An HttpServlet This example creates a simple servlet that sends a form back to the client machine. When a login name is submitted from the SampleHttpServlet.html page, the doGet() method of the servlet is called. This method returns a form to the user requesting address information. When the information is entered and the Submit button is pressed, the doPost() method of the servlet is called. This method returns a confirmation of the information that was entered.
       import javax.servlet.*;
       import javax.servlet.http.*;
       import java.io.*;
       public class SampleHttpServlet extends HttpServlet {
         String login, name, address, city, state, zipcode;
         // The doGet() method is called when the user presses the
         // "Submit" button in the SampleHttpServlet.html Web page.
         // It reads the login name from the query string and sends
         // a form back to the client.
         public void doGet(HttpServletRequest request,
         HttpServletResponse response) throws ServletException,
         IOException {
         // Extract the login name from query string
         login = request.getParameter("login");
         // Set the response type to html

    response.setContentType("text/html");
         // Open a character output stream to the client machine and send a
         // form back to the client browser
         PrintWriter pw = response.getWriter();
         pw.println("<HTML> <HEAD> <TITLE>" + name
         + " information </TITLE>");

    pw.println("</HEAD><BODY>");
         pw.println("<FORM METHOD=POST>");
         pw.println("Enter mailing address <BR><BR>");
         pw.println("<TABLE>");
         pw.println("<TR><TD> Name </TD>");
         pw.println("<TD><INPUT TYPE=TEXT NAME=name> </TD></TR>");

         pw.println("<TR><TD> Address </TD>");
         pw.println("<TD><INPUT TYPE=TEXT NAME=address SIZE=20></TD></TR>");
         pw.println("<TR><TD> City </TD>");
         pw.println("<TD> <INPUT TYPE=TEXT NAME=city></TD></TR>");
         pw.println("<TR><TD> <SELECT NAME=state SIZE=1>");
         pw.println("<OPTION VALUE=AZ>AZ</OPTION>");
         pw.println("<OPTION VALUE=CA>CA</OPTION>");
         pw.println("<OPTION VALUE=NY>NY</OPTION>");

    pw.println("</SELECT></TD></TR>");
         pw.println("<TR><TD> Zip Code </TD>");
         pw.println("<TD> <INPUT TYPE=TEXT NAME=zipcode></TD></TR>");
         pw.println("</TABLE>");
         pw.println("<INPUT TYPE=SUBMIT VALUE=Submit>");
         pw.println("</FORM></BODY></HTML>");
         pw.close();
         }
         // The doPost() method is called when the user submits the form.
         // It extracts the address information and returns a confirmation
         // back to the user.
         public void doPost(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException,
         IOException {
         name = request.getParameter("name");
         address = request.getParameter("address");
         city = request.getParameter("city");
         state = request.getParameter("state");
         zipcode = request.getParameter("zipcode");

    response.setContentType("text/html");
         // Open a character output stream to the client machine and write
         // the response
         PrintWriter pw = response.getWriter();
         pw.println("<HTML> <HEAD> <TITLE>" + name
         + " information </TITLE>");

    pw.println("</HEAD><BODY>");
         pw.println("Current mailing address <BR><BR>");
         pw.println(name + "<BR>");
         pw.println(address + "<BR>");
         pw.println(city + ", " + state + "  " + zipcode + "<BR>");

    pw.println("</BODY></HTML>");
         pw.close();
         }
       }
The SampleHttpServlet.html code is the following:
       <HTML>
       <HEAD>
       <TITLE> HttpServlet Example </TITLE>
       </HEAD>
       <BODY>

       <FORM METHOD=GET
         ACTION="http://localhost:8080/servlet/SampleHttpServlet">
       Enter login name
       <INPUT TYPE=TEXT NAME=login ><BR>
       <INPUT TYPE=SUBMIT VALUE=Submit>
       </BODY>
       </HTML>

HttpServlet() |
public HttpServlet() | Constructor
This constructor does nothing. Because HttpServlet is an abstract class, an HttpServlet object is never created directly.

doDelete() |
protected void doDelete(HttpServletRequest request, HttServletResponse response) throws ServletException, IOException | Method
doDelete() is called by the server via the service() method to handle an HTTP DELETE request. A DELETE request allows a client to remove a document or Web page from a server.

doGet() |
protected void doGet(HttpServletRequest request, HttServletResponse response) throws ServletException, IOException | Method
doGet() is called by the server via the service() method to handle an HTTP GET request. A GET request allows a client to send form data to a server. With the GET request, the form data is attached to the end of the URL sent by the browser to the server as a query string. The amount of form data that can be sent is limited to the maximum length the URL can be.

doOptions() |
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | Method
doOptions() is called by the server via the service() method to handle an HTTP OPTIONS request. An OPTIONS request determines which HTTP methods the server supports and sends the information back to the client by way of a header.

doPost() |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | Method
doPost() is called by the server via the service() method to handle an HTTP POST request. A POST request allows a client to send form data to a server. With the POST request, the form data is sent to the server separately instead of being appended to the URL. This allows a large amount of form data to be sent.

doPut() |
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | Method
doPut() is called by the server via the service() method to handle an HTTP PUT request. A PUT request allows a client to place a file on the server and is conceptually similar to sending the file to the server via FTP.

doTrace() |
protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | Method
doTrace() is called by the server via the service() method to handle an HTTP TRACE request. A TRACE request returns the headers sent with the TRACE request back to the client. This can be useful for de-bugging purposes. This method is rarely overridden.

getLastModified() |
protected long getLastModified(HttpServletRequest request) | Method
getLastModified() returns the time the requested resource was last modified. The return value is the time in milliseconds since midnight Jan 1, 1970.

service() |
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | Method
service() receives HTTP requests and sends them to the appropriate do() method. This method is generally not overridden.