Forward Vs Redirect in Servlets

Hello,I am trying to understand when it is best to use a forward and when a redirect. I know their differences but I can't find an example on when I need to use "forward". Any reason or example will be appreciated.Thanx
[241 byte] By [johndel6a] at [2007-10-2 6:22:49]
# 1
What are you confused about?
filestreama at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Why would someone use "forward"? Just because the browser doesn't know about the transition and because the data are forwarded with a forward?
johndel6a at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
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.
filestreama at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
You mean this two:new RequestDispatcher().forward()request.getRequestDispatcher().forward()Isn't it the same thing? (where the latter is the one that should be used?
johndel6a at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> 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?

filestreama at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Basically I can understand when to use redirect and include but I dont know when forward is to be used?Also, if you hava many forwards in an application, isn't there a security problem? (If servlets are not protected)
johndel6a at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

>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

evnafetsa at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

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.

filestreama at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

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

deepak.tewari@ubicsindia.com at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

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.

johndel6a at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
NOTE: the ****** above is a malicious person trying to exploit your application. I don't know why c-racker is starred?!!
johndel6a at 2007-7-16 13:24:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...