> Whose forward(), RequestDispatcher or
> HttpServletRequest?
>
> What I am getting at is that there are several
> mechanisms by which one can "change flow" of the
> response that will be generated.
Sorry. Let me be clear.
RequestDispatcher allows one to use either forward() or include(). Is this where your confusion lies?
>Why would someone use "forward"? Just because the browser
>doesn't know about the transition and because the data are forwarded
>with a forward?
Thats pretty much it.
Using forward means that you can split up the responsibility for handling the requests among several components.
You can also decide to forward to one page in one condition, and a different page in another.
A common pattern you see in many frameworks (struts, JSF...) is for a single controller servlet to be invoked. That controller runs some business logic, and then forwards to an appropriate result page - making the entire request look seamless.
The advantages of using a forward over a redirect
- doesn't require a round trip for the client to reissue a new request
- retains all request attributes/parameters (lost on redirect)
disadvantage:
- can be confusing for relative urls from your pages. ie you request "/saveUser" which forwards to "/jsp/user/saveSuccess.jsp". Relative links would be resolved from "/saveUser" which can be confusing. Solution - use an html <base> tag
- the "refresh" problem.
Advantages of using redirect over a forward
- when you "refresh" the page it only refreshes the page that was redirected to. Often after a "save" request, you redirect to a "display" page. If that page is refreshed it just redoes the "display", rather than "save and display" which is what would happen if you used a forward.
- no confusion over relative links.
Forward and Redirect both have their uses. The trick is knowing when to use which ;-)
Hope that explains things a bit.
Cheers,
evnafets
RequestDispatcher.forward() is similar to include(). forward() allows a servlet to partially process the request and then pass it on to another servlet(s) for further or final processing.
because forward() is handled on the server whereas sendRedirect() sends a "message" to the browser, forward() is transparent to the client.
Hi,
see in both the case ultimately u reach to the jsp page
but if u require to retain the parameters stored in the request
and the jsp is of kind display and needs a lot of information
from the previous jsp then it is safe to use forward that is the
values in the request are retained and passed to the next URL.
But,
if u just want an input screen and don't need much information from
the previous jsp use redirect.
This is my guess I am not pretty too sure
My main issue for starting this topic is that I think that forward raises
serious security issues and it is not worth mainting it. Data that are not passed with a redirect can easily be passed through the session object. With a forward you assume that a user will not be aware of the intermediate servlets because his browser is not aware. What will happen in the case that a ******* is aware of your servlets?
If you then want to maintain security, you need to maintain it to multiple servlets instead of just 1. E.G. It is not possible to have a Security controller that forward requests, because it can be bypassed and reverse engineering may be done at the form checker servlet.
In order to handle big applications, excessive use of beans could be used, so instead of forward to another servlet, you could use a bean that check a form, formats the response and returns to the servlet.
I honestly still can't undestand why to use forward except since all of it's pros can be done with either include ot redirect.
Thanx again....
John
Ps.I do not have great experience in web design so please comment on the above comments instead of thinking that this is actually the case. It may or it may not be so.