Core HTTP Servlet API
As you can see from Figure 3.4, when the HttpServlet receives a request from the browser, the service() method in the servlet is called, which in turn calls either the doGet() or doPost() methods in the servlet, depending on the type of HTTP request (GET or POST). The request data is received by the servlet from the browser as URL-encoded name-value pairs.
Figure 3.4 HTTP servlet request model.
The service() Method
This method is called for every request received by the HttpServlet from the browser. It is optional for a servlet to override this method.
The doGet() Method
This method is called for every HTTP GET request received by the servlet from the browser. All application code to process the GET requests should be written in this method. The doGet() method receives a prepopulated HttpServletRequest object. A second parameter received by this method is the HttpServletResponse object. The doGet() method processes the input from the browser from the HttpServletRequest object. The doGet() method sends the response back to the browser using the HttpServletResponse object. The doGet() method should be overridden by your servlet.
The doPost() Method
This method is called for every HTTP POST request received by the servlet from the browser. All application code to process the POST requests should be written in this method. The doPost() method receives a prepopulated HttpServletRequest object. A second parameter received by this method is the HttpServletResponse object. The doPost() method processes the input from the browser from the HttpServletRequest object. The doPost() method sends the response back to the browser using the HttpServletResponse object. The doPost() method should be overridden by your servlet.
HttpServletRequest
The HttpServletRequest encapsulates the HTTP request received by the servlet from the browser. It is the responsibility of the servlet engine present in the WebLogic Server to prepopulate this object with the request data when passing these as parameters to the doGet() and doPost() methods. The HttpServletRequest object provides a set of methods for retrieving the request data as well as retrieving the reference to the HttpSession object. The HttpServletRequest object also provides a method to retrieve the input stream from the browser to the servlet. This enables the servlet to process input data from the input stream if required.
The HttpServletRequest object has the following methods:
java.util.Enumeration getParameterNames()
The servlet can use this method to retrieve all the parameter names received in the request from the browser.
getParameter(String paramaterName)
The servlet uses this method to retrieve the value of a specific parameter. The parameter name whose value is to be retrieved is passed to this method.
String [] getParameterValues(String parameterName)
If multiple values for a parameter (such as a checkbox or a multiple-selection box) are received in the request, the servlet can use this method to extract an array of these values for the parameter.
getMethod()
A servlet can use this method to detect the type of the request. The method returns a String object containing either a GET or POST (or PUT).
getSession()
This method can be used by the servlet to retrieve an HttpSession object. The HttpSession object can be used for state maintenance, as you shall see throughout this day.
HttpServletResponse
The HttpServletResponse encapsulates the HTTP response sent by the servlet to the browser. It is the responsibility of the servlet engine present in the WebLogic Server to prepopulate this object with the output stream from the servlet to the browser. This enables the servlet to send output data from the servlet to the browser.
The HttpServletResponse object has the following methods:
getWriter()
This method is used by the servlet to retrieve a handle of the output stream to the browser. The servlet uses this method to retrieve a PrintWriter object that can be used to write the actual output to the browser.
setContentType(String mimeType)
This method is used by the servlet to set the MIME type of the response data sent to the browser. The browser uses this MIME type to interpret the data being received from the servlet. For example, if the servlet is sending HTML data to the browser, it calls this method as follows:
res.setContentType("text/html");
where res is an instance of the HttpServletResponse class.