![if !IE]> <![endif]>
Handling HTTP post Requests
An HTTP post request is often used to post data from an HTML form to a server-side form handler that processes the data. For example, when you respond to a Web-based survey, a post request normally supplies the information you specify in the HTML form to the Web server.
Browsers often cache (save on disk) Web pages so they can quickly reload the pages. If there are no changes between the last version stored in the cache and the current version on the Web, this helps speed up your browsing experience. The browser first asks the server if informa-tion of Fig. 30.15 is identical to the servlet of Fig. 30.12, except that it defines a doPost method (line 12) to respond to post requests rather than a doGet method. The default functionality of doPost is to indicate a “Method not allowed” error. We override this method to provide custom post request processing. Method doPost receives the same two arguments as doGet.
// Fig. 9.15: WelcomeServlet3.java
// Processing post requests containing data.
package com.deitel.advjhtp1.servlets;
4
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class WelcomeServlet3 extends HttpServlet {
// process "post" request from client
protected void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
String firstName = request.getParameter( "firstname" );
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
// send XHTML page post.15 WelcomeServlet3 responds to a post request that contains data.
The WelcomeServlet3.html document (Fig. 30.16) provides a form (lines 13– 21) in which the user can input a name in the text input element firstname (line 17), then click the Submit button to invoke WelcomeServlet3. When the user presses the Submit button, the values of the input elements are sent to the server as part of the request. However, note that the values are not appended to the request URL. Note that the form’s method in this example is post. Also, note that a post request cannot be typed into the browser’s Address or Location field and users cannot bookmark post requests in their browsers.
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"">
<!-- Fig. 9.16: WelcomeServlet3.html -->
<html xmlns = "">
<head>
<title>Handling an HTTP Post Request with Data</title>
</head>
<body>
<form action = "/advjhtp1/welcome3" method = "post">
<p><label>
Type your first name and press the Submit button
<br /><input type = "text" name = "firstname" />
<input type = "submit" value = "Submit" />
</label></p>
</form>
</body>
</html>
Fig. 30.16 HTML document in which the form’s action invokes Welcome-Servlet3 through the alias welcome3 specified in web.xml
We use our advjhtp1 context root to demonstrate the servlet of Fig. 30.15. Place WelcomeServlet3.html in the servlets directory created in Section 30.3.2. Place
WelcomeServlet3.class in the classes subdirectory of WEB-INF in the advjhtp1 context root. Then, edit the web.xml deployment descriptor in the WEB-INF directory to include the information specified in Fig. 30.17. Restart Tomcat and type the following URL in your Web browser:
Type your name in the text field of the Web page, then click Submit to invoke the servlet.
Related Topics
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.