1 // Fig. 26.18: RedirectServlet.java
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 java.io.IOException;
8 import java.io.PrintWriter;
9
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" );
18
19 if ( location != null )
20 {
21 if ( location.equals( "deitel" ) )
22 response.sendRedirect( "http://www.deitel.com" );
23 else if ( location.equals( "welcome1" ) )
24 response.sendRedirect( "welcome1" );
25 } // end if
26
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();
31
32 // start XHTML document
33 out.println( "" );
34
35 out.printf( "%s%s%s" , " ,
36 " "-//W3C//DTD XHTML 1.0 Strict//EN"",
37 " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
" );
38
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 mb4 px3">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
6
7
|
Redirecting a Request to Another Site 10 11 12 |
13
Click a link to be redirected to the appropriate page
14
15 href = "/jhtp6/redirect?page=deitel"> 16 www.deitel.com
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:
http://localhost:8080/jhtp6/servlets/RedirectServlet.html
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
|
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.
|