Sometimes it is useful to redirect a request to a different resource. For example, a servlet could determine the type of the client browser and redirect the request to a Web page that was designed specifically for that browser. This technique is also used to redirect browsers to an error page when handling a request fails. The RedirectServlet in Fig. 26.18 receives a page parameter as part of a get request, then uses that parameter to redirect the request to a different resource.
Figure 26.18. Redirecting requests to other resources.
(This item is displayed on pages 1262 - 1263 in the print version)
""Invalid page
Line 17 obtains the page parameter from the request. If the value returned is not null, the nested if... else if statement at lines 2124 determines whether the value is either "deitel" or "welcome1". If the value is "deitel," the response object's sendRedirect method (line 22) redirects the request to www.deitel.com. If the value is "welcome1," line 24 redirects the request to the servlet of Fig. 26.7. Note that line 24 does not explicitly specify the jhtp6 context root for our Web application. When a servlet uses a relative path to reference another static or dynamic resource, the servlet assumes the same base URL and context root as the one that invoked the servletunless a complete URL is specified for the resource. So line 24 actually is requesting the resource located at
http://localhost:8080/jhtp6/welcome1
Similarly, line 51 is actually requesting the resource located at
http://localhost:8080/jhtp6/servlets/RedirectServlet.html
Software Engineering Observation 26.5
Using relative paths to reference resources in the same context root makes your Web application more flexible. For example, you can change the context root without making changes to the static and dynamic resources in the application. |
Once method sendRedirect executes, processing of the original request by the RedirectServlet terminates. If method sendRedirect is not called, the remainder of method doGet outputs a Web page indicating that an invalid request was made. The page allows the user to try again by returning to the XHTML document of Fig. 26.19. Note that one of the redirects is sent to a static XHTML Web page and the other is sent to a servlet.
Figure 26.19. RedirectServlet.html document to demonstrate redirecting requests to other resources.
(This item is displayed on page 1264 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 |
Redirecting a Request to Another Site 10 11 12 | 13
Click a link to be redirected to the appropriate page 1415 href = "/jhtp6/redirect?page=deitel"> 16 www.deitel.com |
RedirectServlet.html (Fig. 26.19) provides two links (lines 1516 and 1718) that allow the user to invoke the servlet RedirectServlet. Each link specifies a page parameter as part of the URL. To demonstrate passing an invalid page, you can type the URL into your browser with no value for the page parameter.
We use our jhtp6 context root to demonstrate the servlet of Fig. 26.18. Place RedirectServlet.html in the servlets directory created in Section 26.4.1. Place Redirect-Servlet.class in the classes subdirectory of WEB-INF in the jhtp6 context root. Then, edit the web.xml deployment descriptor in the WEB-INF directory to include the information specified in Fig. 26.20. Restart Tomcat, and type the following URL in your Web browser:
http://localhost:8080/jhtp6/servlets/RedirectServlet.html
Click a link in the Web page to invoke the servlet.
Descriptor element |
Value |
---|---|
servlet element |
|
servlet-name |
redirect |
description |
Redirecting to static Web pages and other servlets. |
servlet-class |
RedirectServlet |
servlet-mapping element |
|
servlet-name |
redirect |
url-pattern |
/redirect |
When redirecting requests, the request parameters from the original request are passed as parameters to the new request. Additional request parameters can also be passed. For example, the URL passed to sendRedirect could contain name-value pairs. New parameters are added to the existing parameters. A new parameter with the same name as an existing parameter takes precedence over the original value. However, all the values are still passed. In this case, the complete set of values for a given parameter name can be obtained by calling method getParameterValues from interface HttpServletRequest. This method accepts the parameter name as an argument and returns an array of strings containing the parameter values in most-recent-to-least-recent-order.
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