usage of <jsp:forward >


Web tier: servlets, JSP, Web frameworks: usage of <jsp:forward >

  1. usage of <jsp:forward > (2 messages)

    Hi all,

    Can any one tell that where exactly is the usage of
    <jsp:forward > tag, where it can be used in web applications?

    Thanks in advance.

    Threaded Messages (2)

  2. usage of <jsp:forward >[ Go to top ]

    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.
  3. usage of <jsp:forward >[ Go to top ]

    Thanks Marc,

    For the nice brief and clearing the doubts

    thanks again