Difference between <jsp:forward> and response.sendRedirect()

  • With response.sendRedirect(), the browser is asked to go get another page. All HTTP parameters of the original request are lost. The browser’s location bar changes.
  • <jsp:forward> is more efficient. It forwards the request to the specified JSP page on the server side, without asking the browser to generate a new request. You can only forward to resources served by your application server! It also keeps the state.
    The browser’s location bar doesn’t change.

    To test it out, check out
    response.sendRedirect()
    http://www.javaspace.com/docs/DoRedirect.jsp?orderID=100
    <jsp:forward>
    http://www.javaspace.com/docs/DoForward.jsp?orderID=100

    DoRedirect.jsp:

    <%
       response.sendRedirect("/docs/ResultingPage.jsp");
    %>
    

    DoForward.jsp:

    <jsp:forward page="ResultingPage.jsp"/>
    

    ResultingPage.jsp:

    The is the JSP page <i>ResultingPage.jsp</i> the forwarding or redirection.
    <br>
    <br>
    Did the browser's location bar change?
    <br><br>
    The parameter <i>orderID</i> is set to <%=request.getParameter("orderID")%>.