Featured Post

Event Sourcing Video from Michael Ploed

Event Sourcing I want to share a great video I found few days ago that describes very well what Event Sourcing is.

Thursday, September 27, 2012

Forward, Include and Redirect

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


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.


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.


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.


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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">





public class HiServlet extends HttpServlet {

 private static final long serialVersionUID = 6167525405783177839L;

 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  String method = req.getParameter("method"); 
  req.setAttribute("hi", "good morning");
         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")){



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




  String hi=(String)request.getAttribute("hi");
  String method=(String)request.getParameter("method");
  String changeContentType=(String) request.getParameter("changeContentType");
    response.setHeader("Content-Type", changeContentType);


No comments :

Post a Comment