Suggested Pages

Thursday, September 27, 2012

Forward, Include and Redirect

In this post we'll see the differences among the techniques of forward, include and redirect .

Forward


The method forward of RequestDispatcher, allows the server to literally forward the request to another resource (same server or same domain) maintaining all request's parameters. It's usually used when you want the request to be served by another resource (Servlet or JSP) in place of the calling Servlet.

Include


The method include of RequestDispatcher, allows the server to include the contents of the specified resource in the flow of the servlet response. The included resource cannot change:
  • The response status code;
  • The HTTP headers.
Any attempt to modify headers is ignored.

Redirect


The method sendRedirect of HttpServletResponse, tells to the client that it has to send a new request to the pointed path. Therefore it causes two requests to the server from the client. It's used generally to redirect towards other servers and domains.

Example


In the following snippets of code, you have a complete example of how forward, include and redirect work. You can notice that:
  • If you click the anchor Forward, HiServlet forwards to hi.jsp and the printed output is:
    hi=good morning method=forward

  • If you click the anchor Incluce, HiServlet includes hi.jsp and the printed output is:
    hi=good morning method=forward

  • If you click the anchor Redirect, HiServlet tells the client to do another request to the URL http://<host>/hiServlet/jsp/hi.jsp. The printed output is:
    hi=null method=null

  • If you click the anchor Forward & ChangeHeader, HiServlet forwards to hi.jsp and the header Content-Type is set to application/octet-stream:

  • If you click the anchor Include & ChangeHeader, HiServlet includes hi.jsp and the header Content-Type is not set to application/octet-stream

  • If you click the anchor Redirect & ChangeHeader, HiServlet redirects to hi.jsp and the header Content-Type is not set to application/octet-stream
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>hiServlet</display-name>
 
 <servlet>
  <servlet-name>hiServlet</servlet-name>
  <servlet-class>com.simonefolinoblogspot.servlet.HiServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>hiServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
  <welcome-file>/jsp/index.jsp</welcome-file>
 </welcome-file-list>

</web-app>

HiServlet

...

public class HiServlet extends HttpServlet {

 private static final long serialVersionUID = 6167525405783177839L;

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  
  System.out.println("doGet()--start");
  
  String method = req.getParameter("method"); 
 
  System.out.println("method="+method);
  
  req.setAttribute("hi", "good morning");
  
        if(method.equals("forward")){
         RequestDispatcher requestDispatcher = req.getRequestDispatcher("/jsp/hi.jsp"); 
         requestDispatcher.forward(req, resp);
        
        }else if(method.equals("include")){
         RequestDispatcher requestDispatcher = req.getRequestDispatcher("/jsp/hi.jsp");
         requestDispatcher.include(req, resp);
         
        }
        else if (method.equals("redirect")){
         resp.sendRedirect("/hiServlet/jsp/hi.jsp");         
        }
        else{
         resp.sendError(500);
        }
        System.out.println(req.getHeader("Content-Type"));
        System.out.println("doGet()--end");
 }

}

index.jsp
<html>
<head>
<title>EncodePageExample</title>
</head>

<body>
 <ul>
  <li><a href="/hiServlet/hi.do?method=forward">Forward</a></li>
  <li><a href="/hiServlet/hi.do?method=include">Include</a></li>
  <li><a href="/hiServlet/hi.do?method=redirect">Redirect</a></li>
  
  <li><a href="/hiServlet/hi.do?method=forward&changeContentType=application/octet-stream">Forward & ChangeHeader</a></li>
  <li><a href="/hiServlet/hi.do?method=include&changeContentType=application/octet-stream">Include & ChangeHeader</a></li>
  <li><a href="/hiServlet/hi.do?method=redirect&changeContentType=application/octet-stream">Redirect & ChangeHeader</a></li>
  
  
 </ul>
</body>

</html>
hi.jsp

<html>
<head>
 <title>DispatcherExample</title>
</head>

<body>

 <%
  String hi=(String)request.getAttribute("hi");
  String method=(String)request.getParameter("method");
  out.println("hi="+hi);
  out.println("method="+method);
  String changeContentType=(String) request.getParameter("changeContentType");
  if(changeContentType!=null){
    response.setHeader("Content-Type", changeContentType);
  }
 %>
 
</body>

</html>

No comments :

Post a Comment

Suggested Pages