Thursday, July 28, 2016

Handling Http Request & Responses in Servlets

Handling Http Request & 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( ). The GET and POST requests are commonly used when handling form input.

Handling HTTP GET Requests:

The following programs are for servlets that handles 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 Get.html and a servlet is defined in GetServlet.java. The HTML source code for Get.htm 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.

Get.html:

<html>
<body>
<center>
<form name="Form1"
action="http://localhost:9999/servlet/GS">
<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 GetServlet.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.

GetServlet.java:


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetServlet 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 and perform these steps to test this example:
1. Start Tomcat, if it is not already running.
2. Display the Web page in a browser.



3. Select a color.



4. 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 green option and submits the form. The URL sent from the browser to the server is 

                    http://localhost:9999/servlet/GS?color=Green

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




Handling HTTP POST Requests:

 The servlet is invoked when a form on a Web page is submitted. The example contains two files. A
Web page is defined in Post.html and a servlet is defined in PostServlet.java.The HTML source code for Post.html is shown in the following listing. It isidentical toPost.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.

Post.html:

<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:9999/servlet/PS">
<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>

PostServlet.java:


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.





Note: Parameters for an HTTP POST request are not included as part of the URL that
is sent to the Web server. In this example, the URL sent from the browser to the server

             http://localhost:9999/servlet/PS

The parameter names and values are sent in the body of the HTTP request.




Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home