Handling HTTP post Requests

An HTTP post request is frequently used to send 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 type in the form to the Web server.

Browsers often cache (save on disk) Web pages so they can quickly reload them. 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 whether the document has changed or expired since the date the file was cached. If not, the browser loads the document from the cache. Thus, the browser minimizes the amount of data that must be downloaded for you to view a Web page. Browsers typically do not cache the server's response to a post request, because the next post might not return the same result. For example, in a survey, many users could visit the same Web page and respond to a question. The survey results could then be displayed for the user. Each new response changes the overall results of the survey.

When you use a Web-based search engine, the browser normally supplies the information you specify in an HTML form to the search engine with a get request. The search engine performs the search, then returns the results to you as a Web page. Such pages are often cached by the browser in case you perform the same search again. As with post requests, get requests can supply parameters as part of the request to the Web server.

The WelcomeServlet3 servlet in Fig. 26.15 is identical to the servlet in Fig. 26.12, except that it defines a doPost method (lines 1348) rather than a doGet method to respond to post requests. The default functionality of doPost is to indicate a "Method not supported" error. We override this method to provide custom post request processing. Method doPost accepts the same two arguments as doGetan object that implements interface HttpServletRequest to represent the client's request and an object that implements interface HttpServletResponse to represent the servlet's response. As with doGet, method doPost throws a ServletException if it is unable to handle a client's request and throws an IOException if a problem occurs during stream processing.

Figure 26.15. WelcomeServlet3 responds to a post request containing data.

(This item is displayed on pages 1259 - 1260 in the print version)

""Processing post requests with data

 1 // Fig. 26.15: WelcomeServlet3.java
 2 // Processing post requests containing data.
 3 import javax.servlet.ServletException; 
 4 import javax.servlet.http.HttpServlet; 
 5 import javax.servlet.http.HttpServletRequest; 
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.IOException;
 8 import java.io.PrintWriter;
 9
10 public class WelcomeServlet3 extends HttpServlet
11 {
12 // process "post" request from client 
13 protected void doPost( HttpServletRequest request, 
14  HttpServletResponse response ) 
15  throws ServletException, IOException 
16 {
17 String firstName = request.getParameter( "firstname" );
18
19 response.setContentType( "text/html" );
20 PrintWriter out = response.getWriter();
21
22 // send XHTML page to client
23 
24 // start XHTML document
25 out.println( "" );
26
27 out.printf( "%s%s%s" , " ,
28 " "-//W3C//DTD XHTML 1.0 Strict//EN"",
29 " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
" );
30
31 out.println( "
); 32 33 // head section of document 34 out.println( " ); 35 out.println( 36 " " ); 37 out.println( "" ); 38 39 // body section of document 40 out.println( " ); 41 out.println( "

Hello " + firstName + ",
" ); 42 out.println( "Welcome to Servlets!

" ); 43 out.println( "" ); 44 45 // end XHTML document 46 out.println( "" ); 47 out.close(); // close stream to complete the page 48 } // end method doPost 49 } // end class WelcomeServlet3

WelcomeServlet3.html (Fig. 26.16) provides a form (lines 1319) in which the user can input a name in the text input element firstname (line 16), 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. The form's method in this example is post, but 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.

We use our jhtp6 context root to demonstrate the servlet in Fig. 26.15. Place WelcomeServlet3.html in the servlets directory created in Section 26.4.1. Place WelcomeServlet3.class in the classes subdirectory of WEB-INF in the jhtp6 context root. Then, using the information specified in Fig. 26.17, edit the web.xml deployment descriptor in the WEB-INF directory. Restart Tomcat and type the following URL in your Web browser:

 http://localhost:8080/jhtp6/servlets/WelcomeServlet3.html

 

Type your name in the text field of the Web page, then click Submit to invoke the servlet.

Figure 26.16. HTML document in which the form's action invokes WelcomeServlet3 tHRough the alias welcome3 specified in web.xml.

(This item is displayed on pages 1260 - 1261 in the print version)

"http://www.w3.org/1999/xhtml"> 8 9

 1  "1.0"?>
 2  "-//W3C//DTD XHTML 1.0 Strict//EN"
 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4
 5 
 6
 7 
Handling an HTTP Post Request with Data 10 11 12 13 "/jhtp6/welcome3" method = "post" > 14

15 Type your first name and press the Submit button 16
"text" name = "firstname" /> 17 "submit" value = "Submit" /> 18

19 20 21

Figure 26.17. Deployment descriptor information for servlet WelcomeServlet3.

Descriptor element

Value

servlet element

 

servlet-name

welcome3

description

Handling HTTP post requests with data.

servlet-class

WelcomeServlet3

servlet-mapping element

 

servlet-name

welcome3

url-pattern

/welcome3


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net