18 - JSP Request Redirect and Forward

18.1 Overview of JSP Request Redirect and Forward

In a Jva based web application, there are multiple screens and servlets and together they form a web application. In this chapter we will discuss how to forward a control from a servlet or JSP to another JSP or servlet.

18.2 Approaches

There are two approaches with which a JSP can pass the control to another servlet or JSP or to outside the web application.

a) Request forward

b) Redirect

18.3 JSP Request Forward

With request forward, a JSP can forward the control to resources available within the web application. That means which Request forward, JSP can forward the request to another JSP which are part of same web application.

This transfer of control is done by the container internally and browser or client is not involved in the process.

When the forward is done, the original request and response objects are forwarded so attributes available in request will be carried as well.

To Forward a request, RequestDispatcher object is needed which can be obtained either

a) by request object -means the dispatch is relative to the current URL

                RequestDispatcher rd = request.getRequestDispatcher(“url”);

               rd.forward(request,response);

b) by Servlet Context Object - means the dispatch is relative to the root of the ServletContext.

                RequestDispatcher rd = getServletContext.getRequestDispatcher(“url”);

                rd.forward(request,response);

In both cases url is the value where the control needs to be forwarded.

Refer Section 18.6 for examples

18.4 JSP Redirect

This approach generally is used when the control needs to be forwarded outside the web application. For example we need to forward the control to google.com . It does not mean that we cannot use redirect within same web application , but this approach is ideally used to redirect control to different domain.

This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client.

Since it is a new request, the old request and response object is lost.

To redirect a request, sendRedirect(“url”) API needs to be called from response.

response.sendRedirect(“url”);

Another way is using setStatus() and setHeader() methods of response API together.

String Url = "http://www.google.com" ;

response.setStatus(response.SC_MOVED_TEMPORARILY);

response.setHeader("Location",Url);

Refer Section 18.6 for examples.

18.5 Difference between JSP Forward and JSP Redirect

a) JSP Forward is intended to forward a request to resources within the web application where Redirect should be used to send control outside the web application.

b) In forward, request and response objects are forwarded which means attributes stored in request are carried as well where as in case of redirect it is new request so all attributes are lost.

c) In forward, control is forwarded by container and browser is not involved where as in redirect, browser takes the responsibility. To validate this , in case of forward, browser URL is not changed but gets changed while redirect. 

18.6 JSP Request Forward Examples

18.6.1 – Write a program that explains the JSP request forward concept.

Solution – To explain the request forward , let’s

a) create two JSP (JSP1 and JSP2) .

b) JSP1 will print some message on server console and stores some attributes in request and forward the control to JSP2.

c) JSP2 will print the attribute stored by JSP1.

a) Write JSP1.jsp

<html>
  <head>
    <title>JSP 1</title>
  </head>
  <body>
   <%
    System.out.println("Welcome Message : JSP 1");
    request.setAttribute("Request-Attribute", "Value of Attribute ");
    RequestDispatcher rd = request.getRequestDispatcher("/JSP2.jsp");
    rd.forward(request, response);
   %>
  </body>
</html>

b) Write JSP2.jsp

<html>
  <head>
    <title>JSP 2</title>
  </head>
  <body>
    <h3> Welcome Message : JSP 2 </h3>
    <%
      System.out.println("Welcome Message : JSP 2");
      Object attributeValue = request.getAttribute("Request-Attribute");
    %>
    <%=  
      "Request-Attribute is : " + attributeValue
    %>
  </body>
</html>

Testing-

Access JSP1.jsp by hitting the url http://localhost:8080/jsp-tutorial/JSP1.jsp in browser. This will print the messages on server console and value of request attribute. Note that url of browser is not changed and request attribute is available in JSP2.

Server Console will look like below

18.6.2 Write a program to demonstrate our understanding on Redirect?

Solution For this lets write a RedirectJSP.jsp which will

a) Prints the welcome message on server console

b) Stores a request attribute

c) Redirects to JSP2.jsp (created in section 18.6.1 ) which will print welcome message and request attribute value

a) Write RedirectJSP.jsp

<html>
  <head>
    <title>Redirect JSP</title>
  </head>
  <body>
    <%
     System.out.println("Welcome Message : Redirect JSP");
     request.setAttribute("Request-Attribute", "Value of Attribute ");
     response.sendRedirect("JSP2.jsp");
    %>
  </body>
</html>

Testing

Access RedirectJSP.jsp by hitting the url http://localhost:8080/jsp-tutorial/RedirectJSP.jsp in browser. This will print the messages on server console . Note that url of browser is changed and request attribute is not available in JSP2.

Server console will look like below figure

18.6.3 – Change the RedirectJSP written in Section 18.6.2 to redirect to google.com

Solution –

<html>
  <head>
    <title>Redirect JSP</title>
  </head>
  <body>
    <%
      System.out.println("Welcome Message : Redirect JSP");
      request.setAttribute("Request-Attribute", "Value of Attribute ");
      response.sendRedirect("http://www.google.com");
    %>
  </body>
</html>

Testing –

Access RedirectJSP.jsp by hitting the url http://localhost:8080/jsp-tutorial/RedirectJSP.jsp. You will notice that URL in browser will be changed to google.com and Google will be opened.

18.6.4 – Change the RedirectJSP updated in Section 18.6.3 to redirect to google.com but with setStatus() and setHeader() API of response

Solution –

<html>
  <head>
    <title>Redirect JSP</title>
  </head>
  <body>
   <%
     System.out.println("Welcome Message : Redirect JSP");
     request.setAttribute("Request-Attribute", "Value of Attribute ");
     String Url = "http://www.google.com";
     response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
     response.setHeader("Location", Url);
   %>
  </body>
</html>

Testing –

Access RedirectJSP.jsp by hitting the url http://localhost:8080/jsp-tutorial/RedirectJSP.jsp. You will notice that URL in browser will be changed to google.com and Google will be opened.

18.7 Conclusion

In this chapter we learned the different approaches , their usage and difference of forwarding the control from one JSP to another servlet , another jsp/html or to entire new domain.

Like us on Facebook