The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.servlet  [11 examples] > Requests  [6 examples]

e1044. Processing a HEAD Request in a Servlet

By default, a HEAD request is automatically processed by the HttpServlet.doGet() method using a modified response object that simply counts and discards any characters written to the buffer. If it is necessary to improve the performance of a servlet that is frequently accessed with a HEAD request method, you can override the HttServlet.doHead() method and implement a more efficient algorithm. Typically, a doHead() implementation sets the content length and type, as demonstrated by this example.

Note: In the JWSDP 1.0, calling resetBuffer() does nothing in a HEAD request. Therefore, if resetBuffer() is used during servlet processing, a GET and HEAD request may return different content lengths.

    // See also e1035 The Quintessential Servlet
    
    // This method is called by the servlet container to process a HEAD request.
    // There may be many threads calling this method simultaneously.
    public void doHead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // Set the content length and type
        resp.setContentLength(123);
        resp.setContentType("text/html");
    }

 Related Examples
e1040. Getting a Request Parameter in a Servlet
e1041. Preventing Concurrent Requests to a Servlet
e1042. Getting the Requesting URL in a Servlet
e1043. Getting a Request Header in a Servlet
e1045. Getting the Client's Address in a Servlet


© 2002 Addison-Wesley.