Handling HTTP get Requests Containing Data

When requesting a document or resource from a Web server, it is possible to supply data as part of the request. The servlet WelcomeServlet2 in Fig. 26.12 responds to an HTTP get request that contains a name supplied by the user. The servlet uses the name as part of the response to the client.

Figure 26.12. WelcomeServlet2 responds to a get request containing data.

(This item is displayed on page 1256 in the print version)

""Processing get requests with data

 1 // Fig. 26.12: WelcomeServlet2.java
 2 // Processing HTTP get 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;
10 public class WelcomeServlet2 extends HttpServlet
11 {
12 // process "get" request from client
13 protected void doGet( HttpServletRequest request,
14 HttpServletResponse response )
15 throws ServletException, IOException
16 {
17 String firstName = request.getParameter( "firstname" );
19 response.setContentType( "text/html" );
20 PrintWriter out = response.getWriter();
22 // send XHTML document to client
24 // start XHTML document
25 out.println( "" );
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">
" );
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 doGet 49 } // end class WelcomeServlet2

Parameters are passed as name-value pairs in a get request. Line 17 demonstrates how to obtain information that was passed to the servlet as part of the client request. The request object's getParameter method accepts the parameter name as an argument and returns the corresponding String value, or null if the parameter is not part of the request. Note that parameter names are case sensitive and must match exactly. Line 41 uses the result of line 17 as part of the response to the client.

The WelcomeServlet2.html document (Fig. 26.13) provides a form in which the user can input a name in the text input element firstname (line 16) and click the Submit button to invoke WelcomeServlet2. When the user presses the Submit button, the values of the input elements are placed in name-value pairs as part of the request to the server. In the second screen capture in Fig. 26.13, note that the browser appended



to the end of the action URL. The ? separates the query string (i.e., the data passed as part of the get request) from the rest of the URL in a get request. The name-value pairs are passed with the name and the value separated by =. If there is more than one namevalue pair, each pair is separated by &.

Figure 26.13. HTML document in which the form's action invokes WelcomeServlet2 through the alias welcome2 specified in web.xml.

(This item is displayed on page 1257 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">
Processing get requests with data 10 11 12 13 "/jhtp6/welcome2" method = "get" > 14

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

19 20 21

Once again, we use our jhtp6 context root to demonstrate the servlet of Fig. 26.12. Place WelcomeServlet2.html in the servlets directory created in Section 26.4.1. Place WelcomeServlet2.class in the classes subdirectory of WEB-INF in the jhtp6 context root. Remember that classes in a package must be placed in the appropriate package directory structure. Then edit the web.xml deployment descriptor in the WEB-INF directory to include the information specified in Fig. 26.14. This table contains the information for the servlet and servlet-mapping elements that you will add to the web.xml deployment descriptor. You should not type the italic text into the deployment descriptor. Restart Tomcat and type the following URL in your Web browser:



Figure 26.14. Deployment descriptor information for servlet WelcomeServlet2.

(This item is displayed on page 1258 in the print version)

Descriptor element


servlet element





Handling HTTP get requests with data.



servlet-mapping element







Type your name in the text field of the Web page, then click Submit to invoke the servlet. Once again, note that the get request could have been typed directly into the browser's Address or Location field as follows:



Error-Prevention Tip 26.4

If an error occurs during the servlet invocation, the log files in the logs directory of the Tomcat installation can help you determine the error and debug the problem.


Software Engineering Observation 26.4

A get request is limited to standard characters, which means that you cannot submit any special characters via a get request. The length of the URL in a get request is limited. For example, the maximum URL length in Internet Explorer is 2,083 characters. Some Web servers might restrict this even more.


Good Programming Practice 26.1

A get request should not be used for sending sensitive data (e.g., a password) because the form data is placed in a query string that is appended to the request URL as plain text and can be intercepted.


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


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


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


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