The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Servlets
Lesson: Interacting with Clients

## Handling GET and POST Requests

> To handle HTTP requests in a servlet, extend the `HttpServlet` class and override the servlet methods that handle the HTTP requests that your servlet supports. This lesson illustrates the handling of GET and POST requests. The methods that handle these requests are `doGet` and `doPost`.

### Handling GET requests

Handling GET requests involves overriding the `doGet` method. The following example shows the BookDetailServlet doing this. The methods discussed in the Requests and Responses section are shown in bold.

    public class BookDetailServlet extends HttpServlet {

        public void doGet (HttpServletRequest request,
                           HttpServletResponse response)
            throws ServletException, IOException
        {
            ...
    	// set content-type header before accessing the Writer
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();

    	// then write the response
            out.println("<html>" +
                        "<head><title>Book Description</title></head>" +
                        ...);

            //Get the identifier of the book to display
            String bookId = request.getParameter("bookId");
            if (bookId != null) {
                // and the information about the book and print it
                ...
            }
            out.println("</body></html>");
            out.close();
        }
        ...
    }

The servlet extends the `HttpServlet` class and overrides the `doGet` method.

Within the `doGet` method, the `getParameter` method gets the servlet's expected argument.

To respond to the client, the example `doGet` method uses a `Writer` from the `HttpServletResponse` object to return text data to the client. Before accessing the writer, the example sets the content-type header. At the end of the `doGet` method, after the response has been sent, the `Writer` is closed.

### Handling POST Requests

Handling POST requests involves overriding the `doPost` method. The following example shows the ReceiptServlet doing this. Again, the methods discussed in the Requests and Responses section are shown in bold.

    public class ReceiptServlet extends HttpServlet {

        public void doPost(HttpServletRequest request,
                           HttpServletResponse response)
    	throws ServletException, IOException
        {
            ...
            // set content type header before accessing the Writer
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();

            // then write the response
            out.println("<html>" +
                        "<head><title> Receipt </title>" +
                        ...);

            out.println("<h3>Thank you for purchasing your books from us " +
                        request.getParameter("cardname") +
                        ...);
            out.close();
        }
        ...
    }

The servlet extends the `HttpServlet` class and overrides the `doPost` method.

Within the `doPost` method, the `getParameter` method gets the servlet's expected argument.

To respond to the client, the example `doPost` method uses a `Writer` from the `HttpServletResponse` object to return text data to the client. Before accessing the writer the example sets the content-type header. At the end of the `doPost` method, after the response has been set, the `Writer` is closed.

| Start of Tutorial > Start of Trail > Start of Lesson | Search