Discussions

Web tier: servlets, JSP, Web frameworks: sendRedirect v/s forward

  1. sendRedirect v/s forward (5 messages)

    Hi,

    Some info about the response.sendRedirect() and jsp:forward.

    response.sendRedirect() will generate another HTTP request from the client's browser. This means that the servlet is actually handing over the control back to the browser and ask it to generate a fresh request.
    <jsp:forward> actually internally hands over the control to another forwarded page. this way, new request is not generated.
    In other words, response.redirect is executed at the browser but the <jsp:forward> is executed at the web server. sendRedirect is useful when u want to transfre the control to another web site all together, for example. but if u want to transfer the control within your web site or web application, use <jsp:forward> coz it is much faster as it is executed on the server itself. If u wanna give the same effect in servlet, u can user javax.servlet.getServletContext().getRequestDispatcher("jsp or html file name").forward(requestobject,responseobject).

    Threaded Messages (5)

  2. sendRedirect v/s forward[ Go to top ]

    Great summary. Another interesting point to remember
    is the URL in browser's location bar will change
    with response.sendRedirect(), but not with a jsp forward.
    This can confuse QA engineers; they enter bugs on a
    particular JSP, but the one they are looking at isn't the
    one in the location bar.

    Keep it in mind when considering what happens
    if the user hits "back" or "refresh", as well...
    with a forward, the original request (before the forward)
    will happen again in response to a reload.
  3. sendRedirect v/s forward[ Go to top ]

    hi guys

    could u pls eloborate <jsp:include> and <jsp:forward>

    thanx
  4. sendRedirect v/s forward[ Go to top ]

    " <jsp:include> and <jsp:forward> "

    These are JSP "standard actions."
    They are equivalent to the Servlet API's
    RequestDispatcher.forward() and include() methods.

    The main difference is that control returns to
    the calling JSP after completion of the <jsp:include>
    action. Using <jsp:forward>, server-side processing
    terminates after the forwarded resource (you don't
    come back).

    These happen at request-time. Be sure you don't
    confuse the include standard action with the
    include directive. The latter happens at translation time,
    when the source code for the Java Servlet is created
    by the JSP container. The text of the included
    JSP is copied into the includer.
  5. sendRedirect v/s forward[ Go to top ]

    hi,

      I've tried using jsp:forward but encounter a problem.
    How can I add in hidden fields or pass additional parameters into the requestobject of the forward jsp page?

     
  6. sendRedirect v/s forward[ Go to top ]

    From the spec:
    "A jsp:forward action may have jsp:param
    subelements that can provide values for
    some parameters in the request to be used for
    the forwarding."

    and

    <jsp:forward page=” urlSpec”>
    { <jsp:param .... /> }*
    </jsp:forward>