Home | | Web Programming | Handling HTTP Requests and Responses

Chapter: Java The Complete Reference : Applying Java : Introducing Servlets

Handling HTTP Requests and Responses

The HttpServlet class provides specialized methods that handle the various types of HTTP requests. A servlet developer typically overrides one of these methods. These methods are doDelete( ), doGet( ), doHead( ), doOptions( ), doPost( ), doPut( ), and doTrace( ).

Handling HTTP Requests and Responses

 

The HttpServlet class provides specialized methods that handle the various types of HTTP requests. A servlet developer typically overrides one of these methods. These methods are doDelete( ), doGet( ), doHead( ), doOptions( ), doPost( ), doPut( ), and doTrace( ). A complete description of the different types of HTTP requests is beyond the scope of this book. However, the GET and POST requests are commonly used when handling form input. Therefore, this section presents examples of these cases.

 

Handling HTTP GET Requests

 

Here we will develop a servlet that handles an HTTP GET request. The servlet is invoked when a form on a web page is submitted. The example contains two files. A web page is defined in ColorGet.html, and a servlet is defined in ColorGetServlet.java. The HTML source code for ColorGet.html is shown in the following listing. It defines a form that contains a select element and a submit button. Notice that the action parameter of the form tag specifies a URL. The URL identifies a servlet to process the HTTP GET request.

 

<html>

 

<body>

 

<center>

 

<form name="Form1" action="http://localhost:8080/examples/servlets/servlet/ColorGetServlet">

 

<B>Color:</B>

 

<select name="color" size="1"> <option value="Red">Red</option> <option value="Green">Green</option>

<option value="Blue">Blue</option> </select>

<br><br>

 

<input type=submit value="Submit"> </form>

 

</body>

 

</html>

 

The source code for ColorGetServlet.java is shown in the following listing. The doGet( ) method is overridden to process any HTTP GET requests that are sent to this servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the selection that was made by the user. A response is then formulated.

 

import java.io.*; import javax.servlet.*;

import javax.servlet.http.*;

 

public class ColorGetServlet extends HttpServlet {

 

public void doGet(HttpServletRequest request, HttpServletResponse response)

 

throws ServletException, IOException {

 

String color = request.getParameter("color"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("<B>The selected color is: "); pw.println(color);

 

pw.close();

 

}

 

}

 

Compile the servlet. Next, copy it to the appropriate directory, and update the web.xml file, as previously described. Then, perform these steps to test this example:

 

            Start Tomcat, if it is not already running.

 

            Display the web page in a browser.

 

            Select a color.

 

            Submit the web page.

 

After completing these steps, the browser will display the response that is dynamically generated by the servlet.

One other point: Parameters for an HTTP GET request are included as part of the URL that is sent to the web server. Assume that the user selects the red option and submits the form. The URL sent from the browser to the server is

 

http://localhost:8080/examples/servlets/servlet/ColorGetServlet?color=Red

 The characters to the right of the question mark are known as the query string.

Handling HTTP POST Requests

Here we will develop a servlet that handles an HTTP POST request. The servlet is invoked when a form on a web page is submitted. The example contains two files. A web page is defined in ColorPost.html, and a servlet is defined in ColorPostServlet.java.

 

The HTML source code for ColorPost.html is shown in the following listing. It is identical to ColorGet.html except that the method parameter for the form tag explicitly specifies that the POST method should be used, and the action parameter for the form tag specifies a different servlet.

 

<html>

 

<body>

 

<center>

 

<form name="Form1" method="post"

 

action="http://localhost:8080/examples/servlets/servlet/ColorPostServlet">

 

<B>Color:</B>

 

<select name="color" size="1"> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> </select>

<br><br>

 

<input type=submit value="Submit"> </form>

 

</body>

 

</html>

 

The source code for ColorPostServlet.java is shown in the following listing. The doPost( ) method is overridden to process any HTTP POST requests that are sent to this servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the selection that was made by the user. A response is then formulated.

 

import java.io.*; import javax.servlet.*;

import javax.servlet.http.*;

 

public class ColorPostServlet extends HttpServlet {

 

public void doPost(HttpServletRequest request, HttpServletResponse response)

 

throws ServletException, IOException {

 

String color = request.getParameter("color"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("<B>The selected color is: "); pw.println(color);

 

pw.close();

 

}

 

}

Compile the servlet and perform the same steps as described in the previous section to test it.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Applying Java : Introducing Servlets : Handling HTTP Requests and Responses |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.