The HttpServlet class extends the GenericServlet class. Most of the servlet classes that you develop extend the HttpServlet class. To program with HttpServlet, you override the service method or one or more of the following HTTP-specific request handling methods:
doGet
doPostdoPutdoDeletedoHeaddoOptionsdoTraceEach of these methods takes the following parameters:
HttpServletRequest, which contains HTTP headers and other client request information.
HttpServletResponse, which lets you return HTML to the browser.
JRun invokes the service method for every servlet request for servlets that extend GenericServlet. You must override the service method and can optionally override the init and destroy methods.
For servlets that extend HttpServlet, the default implementation of the service method routes requests to the appropriate doXxx method, where Xxx is the type of request (GET or POST). For example, when the servlet receives an HTTP GET request, the service method calls the doGet method. If you override the default service method, the servlet must either process all types of HTTP requests or include logic to dispatch requests to the appropriate doXxx method.
JRun invokes the doGet method for HTTP GET requests. The web browser sends an HTTP GET request when the user types a URL, clicks a link, or submits a form that specifies method=GET. The GET method is the most common method for requesting a page.
The following example shows a servlet that overrides the doGet method:
import java.io.*;
import javax.servlet.*; import javax.servlet.http.*;
public class DisplayInfo extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>Display Information");
out.println("</title></head><body>");
out.println("Request URI: " + req.getRequestURI() + "<br>");
out.println("</body></html>");
}
}
JRun invokes the doPost method for HTTP POST requests. The web browser sends an HTTP POST request when the user submits a form that specifies method=POST.
The following example shows an HTML form that invokes a servlet using a POST request:
<html><head><title>Login to the System</title></head>
<body bgcolor="Silver"> <h1>Login to the System</h1> <!-- Display the login form. --> <form action="/servlet/selectionForm" method="POST"> <p>Name: <input type="Text" name="myName" size="30"> <p> <input type="Submit" value="Log In"> </form> </body> </html>
The following example shows a servlet that uses the doPost method to access a passed value:
import java.io.*;
import javax.servlet.*; import javax.servlet.http.*;
public class SelectionForm extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String thisName = "Unknown Name";
// Get user name from login form.
String[] attrArray = req.getParameterValues("myName");
// We know that the calling form only has one value for myName.
if(attrArray != null) {
thisName = attrArray[0];
}
out.println("<html><head><title>Choose Information to Display");
out.println("</title></head><body>");
out.println("<h1>Welcome " + thisName + "</h2>");
out.println("<h2>Choose Information to Display</h2>");
// Use the backslash to escape double quotes.
out.println("<form action=\"/servlet/displayInfo\" method=\"post\">");
// Checkbox to display HTTP request information
out.println("<p>Display request information? ");
out.println("<input type=\"Checkbox\" name=\"requestInfo\" checked>");
// Checkbox to display cookie information
out.println("<p>Display cookies? ");
out.println("<input type=\"Checkbox\" name=\"showCookies\" checked>");
out.println("<br>");
out.println("<input type=\"Submit\">");
out.println("</form>");
out.println("</body></html>");
}
}
Most servlets respond to doGet methods. However, you want to ensure that your servlet does not return an error if you forget to handle the doPost method and then add a form that posts the request to the servlet. The most common way to avoid this is to pass control to the doGet method from the doPost method by default.
The following example shows a servlet that implements both methods:
import javax.servlet.*;
import javax.servlet.http.*;
public class GetServletConfigInfo extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
... //implement servlet here
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doGet(request, response);
}
}
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
The HttpServlet class includes methods that support additional HTTP request types, as the following table explains. HTTP version 1.1 supports all request types; HTTP version 1.0 supports only GET, HEAD, and POST.