Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Redirecting Requests to Other Resources - Servlets

Chapter: Internet & World Wide Web HOW TO PROGRAM - Servlets: Bonus for Java™ Developers

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Redirecting Requests to Other Resources - Servlets

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.

Redirecting Requests to Other Resources

 

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. The RedirectServlet of Fig. 30.18 re-ceives a page parameter as part of a get request, then uses that parameter to redirect the request to a different resource.

 

            // Fig. 9.18: RedirectServlet.java

            // Redirecting a user to a different Web page.

            package com.deitel.advjhtp1.servlets;

 

            import javax.servlet.*;

            import javax.servlet.http.*;

            import java.io.*;

 

            public class RedirectServlet extends HttpServlet {

 

 

            // process "get" request from client

            protected void doGet( HttpServletRequest request,

            HttpServletResponse response )

            throws ServletException, IOException

            {

String location = request.getParameter( "page" );         

                       

if ( location != null ) 

                       

            if ( location.equals( "deitel" ) )       

            response.sendRedirect( "http://www.deitel.com" );

            else    

            if ( location.equals( "welcome1" ) )          

            response.sendRedirect( "welcome1" );   

      // code that executes only if this servlet

      // does not redirect the user to another page

 

      response.setContentType( "text/html" );

      PrintWriter out = response.getWriter();

 

      // start XHTML document

      out.println( "<?xml version = \"1.0\"?>" );

          out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +

          "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +

          "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

         

          out.println(

          "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

            // head section of documentout.println( "<head>" );

      out.println( "<title>Invalid page</title>" );

      out.println( "</head>" );

      // body section of document

      out.println( "<body>" );

      out.println( "<h1>Invalid page requested</h1>" );

      out.println( "<p><a href = " +

      "\"servlets/RedirectServlet.html\">" );

      out.println( "Click here to choose again</a></p>" );

      out.println( "</body>" );

54

      // end XHTML document

      out.println( "</html>" );

      out.close();  // close stream to complete the page

      }

}

 

 

 

Fig. 30.18  Redirecting requests to other resources

 

Line 16 obtains the page parameter from the request. If the value returned is not null, the if/else structure at lines 20–24 determines if the value is either deitel or welcome1.” If the value is “deitel,” the response object’s sendRedirect method (line 21) redirects the request to www.deitel.com. If the value is “welcome1,” line 24 redirect the request to the servlet of Fig. 30.5. Note that line 24 does not explicitly specify the advjhtp1 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 servlet—unless a complete URL is specified for the resource. So, line 24 actually is requesting the resource located at

 

http://localhost:8080/advjhtp1/welcome1

 

Similarly, line 51 actually is requesting the resource located at

 

http://localhost:8080/advjhtp1/servlets/RedirectServlet.html

 

Once method sendRedirect executes, processing of the original request by the RedirectServlet terminates. If method sendRedirect is not called, the remainder of method doPost 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. 30.19. Note that one of the redirects is sent to a static XHTML Web page and the other is sent to a servlet.

 

The RedirectServlet.html document (Fig. 30.19) provides two hyperlinks (lines 15–16 and 17–18) that allow the user to invoke the servlet RedirectServlet. Note that each hyperlink 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.

 

      <?xml version = "1.0"?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

5  <!-- Fig. 9.19: RedirectServlet.html -->

 

6

      <html xmlns = "http://www.w3.org/1999/xhtml">

      <head>

      <title>Redirecting a Request to Another Site</title>

        </head>

11

        <body>

        <p>Click a link to be redirected to the appropriate page</p>

        <p>

        <a href = "/advjhtp1/redirect?page=deitel">

        www.deitel.com</a><br />

        <a href = "/advjhtp1/redirect?page=welcome1">

        Welcome servlet</a>

        </p>

        </body>

</html>


 

Fig. 30.19 RedirectServlet.html document to demonstrate redirecting requests to other resources.

 

We use our advjhtp1 context root to demonstrate the servlet of Fig. 30.18. Place RedirectServlet.html in the servlets directory created in Section 30.3.2. Place

 

RedirectServlet.class in the classes subdirectory of WEB-INF in the advjhtp1 context root. Then, edit the web.xml deployment descriptor in the WEB-INF directory to include the information specified in Fig. 30.20. Restart Tomcat, and type the following URL in your Web browser:

 

http://localhost:8080/advjhtp1/servlets/RedirectServlet.html

 

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


When redirecting requests, the request parameters from the original request are passed as parameters to the new request. Additional request parameters also can be passed. For example, the URL passed to sendRedirect could contain name/value pairs. Any new parameters are added to the existing parameters. If a new parameter has the same name as an existing parameter, the new parameter value 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 inter-face HttpServletRequest. This method receives the parameter name as an argument and returns an array of Strings containing the parameter values in order from most recent to least recent.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.