Redirecting Requests to Other Resources

Table of contents:

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

 1 // Fig. 26.18:
 2 // Redirecting a user to a different Web page.
 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;
 8 import;
10 public class RedirectServlet 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 location = request.getParameter( "page" );
19 if ( location != null )
20 {
21 if ( location.equals( "deitel" ) )
22 response.sendRedirect( "" );
23 else if ( location.equals( "welcome1" ) )
24 response.sendRedirect( "welcome1" );
25 } // end if
27 // code that executes only if this servlet
28 // does not redirect the user to another page
29 response.setContentType( "text/html" );
30 PrintWriter out = response.getWriter();
32 // start XHTML document
33 out.println( "" );
35 out.printf( "%s%s%s" , " ,
36 " "-//W3C//DTD XHTML 1.0 Strict//EN"",
37 " "">
" );
39 out.println(
40 "
); 41 42 // head section of document 43 out.println( " ); 44 out.println( " " ); 45 out.println( "" ); 46 47 // body section of document 48 out.println( " ); 49 out.println( "

Invalid page requested

" ); 50 out.println( "

<a href="</span> + 51 <span style=">""servlets/RedirectServlet.html">" ); 52 out.println( "Click here to choose again</a>

" ); 53 out.println( "" ); 54 55 // end XHTML document 56 out.println( "" ); 57 out.close(); // close stream to complete the page 58 } // end method doGet 59 } // end class RedirectServlet

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 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



Similarly, line 51 is actually requesting the resource located at



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)

""> 8 9

 1  "1.0" ?>
 2  "-//W3C//DTD XHTML 1.0 Strict//EN"
 3 "">
Redirecting a Request to Another Site 10 11 12 13

Click a link to be redirected to the appropriate page


15 href = "/jhtp6/redirect?page=deitel"> 16
17 href = "/jhtp6/redirect?page=welcome1"> 18 Welcome servlet 19

20 21

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:



Click a link in the Web page to invoke the servlet.

Figure 26.20. Deployment descriptor information for servlet RedirectServlet.

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

Descriptor element


servlet element





Redirecting to static Web pages and other servlets.



servlet-mapping element







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


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 © 2008-2020.
If you may any questions please contact us: