when to use response redirect and jsp forward

Discussions

Web tier: servlets, JSP, Web frameworks: when to use response redirect and jsp forward

  1. hai freinds,

    what are the cases we use the response redirect statement and we move for jsp forward. if we use forward it will go to that page and do process but url will be first only , why this is?

    with regards
    k.ramakrishna
  2. while using response.sendredirect the server sends the response to the client browser, from where the next request comes. so it appears to the server that this is like a fresh request so it displays that url correctly. however in case of response.forward the server does not do this extra task of sending response to the client and then coming back to the server. the server in this case directly sends the response of the forward call to the client. hence also the url remains the same. but as u can see response.sendredirect is slower than response.forward as a round trip to the client is saved in this case.
  3. Hello,

    Both pageContext.forward("") and requestDispatcher.forward("") work in the same way. These two can be used to redirect a request to another relative URL in the same server.

    Whereas response.sendRedirect("http://www.xyz.com") can be used to redirect a request to another URL which is on some other server.

    Regards,
    Ravi Sankar Pabbati
    GE India
  4. You can use the forward tag in a JSP when you want to continue processing the current request with a different JSP page. The forwarding happens on the server, before the output is finished streaming to the client. The forward tag is the same thing as using a RequestDispatcher in Java Servlets (take a look at the "generated Java" Servlet that your application server creates when it compiles your JSP, and you'll see.

    Since this forwarding happens on the server side, before the request processing is completed, the second JSP has access to the same request object. You could, for example, place set an attribute of the request in JSP #1, do a forward to JSP #2, and retrieve the attribute from the "request" implicit object. It's one way of passing data between JSPs.

    This is different from using response.sendRedirect() in a scriptlet; this tells the browser to make a second request. It's another way of getting the user to that second JSP, but in this case, there is a new request object, so you can't pass attributes with this method.

    The forward tag is similar to the include tag, but control doesn't return to JSP #1 with forward. Check out include, however, which has two forms. One just copies the text of the included JSP into yours, and one is a "request-time include", which actually goes and gets JSP #2 while the request is being processed. With these, control does return to JSP #1 after your include (actually, in the first case, it never leaves).

    Here's the syntax for the forward tag from the spec:

    <jsp:forward page=” relativeURLspec” />

    There is also a form with takes parameters.
    Note that "relativeURLspec" can be a static HTML page,
    another JSP, or a Servlet.

    If you read up on them and decide the the Servlet API's response.sendRedirect is what you want instead, one more note of caution: put a "return;" after the sendRedirect(), or server-side processing of your JSP will wastefully go on to the end of the page even though you've sent the client away. No big deal usually, but sloppy. Not a problem with forward, which "effectively terminates the page" (from the spec).

    One other interesting note: since a sendRedirect() is a new request, the browser's location bar is changed to show JSP #2. With a forward, it isn't. This means if you use them a lot, QA people might report bugs on JSP #1 even when the problem happend on JSP #2. Further, if they click "Refresh/Reload" on their browser, guess what happens? JSP #1 fires again, of course.

    So I like redirects better, and use them more often. I use forwards when I want that request object to still be around, because I've set a bunch of attributes on it and JSP #2 needs those attributes.

    Hope this helps.
  5. Thanks a loot for this is a superb explanation of jsp:forward and redirect. I have situation where I have to pass some parameters from a jsp page to a servlet in ANOTHER SERVER. As per your explanation, both jsp:forward and response.redirect will fail in my case. Can you please suggest me what method should I use over here? Thanks.
  6. public interface RequestDispatcher should be used

  7. See this redirect example and forward example