You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Antony Paul <an...@hotmail.com> on 2003/12/01 14:28:26 UTC

forward vs redirect ?

Hi,
    I would like to receive comments on when to use forward and redirect in
action mapping. Do people use forward only. All the articles and examples I
saw uses forward.

Antony Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Caroline Lauferon <ca...@cgey.com>.
hum... tings are not exactly like I described them, but i thought it was the
same.... *.jsp are tiles tiles definitions names, not resources. does it
matter. the URL shown is the action path.

Caroline


> caroline,
>
> when the user gets to page2, what is the url displayed in the address box?
> is it the action or the jsp page?
>
>
> claire
>
> ----- Original Message -----
> From: "Caroline Lauferon" <ca...@cgey.com>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> Sent: Monday, December 01, 2003 4:04 PM
> Subject: Re: forward vs redirect ?
>
>
> > > For example, say i had an action which saved some details from a form
to
> a
> > > database. if the page is not redirected then if the user were to
refresh
> > the
> > > page, the action would be called again and the data saved again. This
> > > obviously i would not want the user to do so in this case i redirect
the
> > > page, so that if the user were to refresh the page, the save action
> would
> > > not be called again.
> >
> > I'm not sure I understand what you mean. The case you describe is very
> close
> > to mine, and so I would like to prevent the user from making twice the
> > addition on refresh:
> > I've got one first form, Form1, which calls Action1:
> > <action path="/action1" name="Form1" type="actions.Action1"
> scope="session"
> > input="/form1.jsp">
> >     <forward name="ok" path="page2.jsp" redirect="true"/>
> > </action>
> >
> > but once the user is on page2, if he makes a refresh, action1 is called
> > again.... and the data added by action1 is added once more....
> >
> > What didn't I understand?
> > Thanks
> > Caroline
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Claire Wall <cl...@kurtosys.com>.
caroline,

when the user gets to page2, what is the url displayed in the address box?
is it the action or the jsp page?


claire

----- Original Message -----
From: "Caroline Lauferon" <ca...@cgey.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, December 01, 2003 4:04 PM
Subject: Re: forward vs redirect ?


> > For example, say i had an action which saved some details from a form to
a
> > database. if the page is not redirected then if the user were to refresh
> the
> > page, the action would be called again and the data saved again. This
> > obviously i would not want the user to do so in this case i redirect the
> > page, so that if the user were to refresh the page, the save action
would
> > not be called again.
>
> I'm not sure I understand what you mean. The case you describe is very
close
> to mine, and so I would like to prevent the user from making twice the
> addition on refresh:
> I've got one first form, Form1, which calls Action1:
> <action path="/action1" name="Form1" type="actions.Action1"
scope="session"
> input="/form1.jsp">
>     <forward name="ok" path="page2.jsp" redirect="true"/>
> </action>
>
> but once the user is on page2, if he makes a refresh, action1 is called
> again.... and the data added by action1 is added once more....
>
> What didn't I understand?
> Thanks
> Caroline
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Caroline Lauferon <ca...@cgey.com>.
Thank you very much Hubert! and Thank you Claire!
it indeed works the way you describe it. I suppose redirect="true" works
only with /sthg.do or sthg.jsp, and not with a tile definition, isn't it?
So, to solve my problem, I should use a /mytiledefinition.do (being a
forward action), like this :

<action path="/action1" name="Form1" type="actions.Action1" scope="session"
input="/form1.jsp">
    <forward name="ok" path="/page2definition.do" redirect="true"/>
</action>
<action path="/page2definition" type="myForwadingAction"/>

Caroline


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Hubert Rabago <ja...@yahoo.com>.
Hi Caroline,

I sent you a struts war file ready to deploy.  It includes all source files. 
Call up the displayItem.do, enter a bid amount, submit the form, then refresh on
the resulting page.  Both action objects do a System.out.println when they're
called.  When you refresh the page after submitting the bid, you'll see that
displayItem.do is the only one being called, and that the address bar will show
the displayItem.do URL.
I ran this in Tomcat 4.1.x.
Please reply to this mailing list so others can help if there are issues.

Here's the mappings:
    <action-mappings>
        <action path="/displayItem"
            type="com.hkrabago.redirect.DisplayItem">
            <forward name="doDisplay" path="/item.jsp"/>
        </action>
        <action path="/processBid"
            type="com.hkrabago.redirect.ProcessBid">
            <forward name="doRedirect" path="/displayItem.do" redirect="true"/>
        </action>
    </action-mappings>

Hubert

--- Caroline Lauferon <ca...@cgey.com> wrote:
> > For example, say i had an action which saved some details from a form to a
> > database. if the page is not redirected then if the user were to refresh
> the
> > page, the action would be called again and the data saved again. This
> > obviously i would not want the user to do so in this case i redirect the
> > page, so that if the user were to refresh the page, the save action would
> > not be called again.
> 
> I'm not sure I understand what you mean. The case you describe is very close
> to mine, and so I would like to prevent the user from making twice the
> addition on refresh:
> I've got one first form, Form1, which calls Action1:
> <action path="/action1" name="Form1" type="actions.Action1" scope="session"
> input="/form1.jsp">
>     <forward name="ok" path="page2.jsp" redirect="true"/>
> </action>
> 
> but once the user is on page2, if he makes a refresh, action1 is called
> again.... and the data added by action1 is added once more....
> 
> What didn't I understand?
> Thanks
> Caroline
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Caroline Lauferon <ca...@cgey.com>.
> For example, say i had an action which saved some details from a form to a
> database. if the page is not redirected then if the user were to refresh
the
> page, the action would be called again and the data saved again. This
> obviously i would not want the user to do so in this case i redirect the
> page, so that if the user were to refresh the page, the save action would
> not be called again.

I'm not sure I understand what you mean. The case you describe is very close
to mine, and so I would like to prevent the user from making twice the
addition on refresh:
I've got one first form, Form1, which calls Action1:
<action path="/action1" name="Form1" type="actions.Action1" scope="session"
input="/form1.jsp">
    <forward name="ok" path="page2.jsp" redirect="true"/>
</action>

but once the user is on page2, if he makes a refresh, action1 is called
again.... and the data added by action1 is added once more....

What didn't I understand?
Thanks
Caroline


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Antony Paul <an...@hotmail.com>.
Thank u for the reply. I followed the same pattern in another non struts
based application. But on reading the struts users guide and articles and
going through the sample sapplication, I found that nobody is mentioning
about redirect and the problem with refresh. So I gott confused whether
there are other ways available.  The sample application is throwing an
exception, when the page displayed adding new subscriptions is refreshed.

Antony Paul

----- Original Message -----
From: "Claire Wall" <cl...@kurtosys.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, December 01, 2003 7:29 PM
Subject: Re: forward vs redirect ?


> Hi Antony,
>
> As a rule i always use just forward unless there is a specific need to
> redirect to the page you're forwarding to.
>
> For example, say i had an action which saved some details from a form to a
> database. if the page is not redirected then if the user were to refresh
the
> page, the action would be called again and the data saved again. This
> obviously i would not want the user to do so in this case i redirect the
> page, so that if the user were to refresh the page, the save action would
> not be called again.
>
> The times when I would advise against using redirecting is when you're
> storing objects on the request object in your action. If you redirect then
> this information is lost. There are ways around this (like putting the
> objects on the session rather than on the request object) but i try to cut
> down on the amount of information being stored in the session.
>
> There are probably some better defined uses of the redirect attribute and
> when/where to use it, but the above is the general rules i go by, having
run
> into problems with both redirecting and not redirecting in the past.
>
> hope this helps
>
> Claire
>
>
> ----- Original Message -----
> From: "Antony Paul" <an...@hotmail.com>
> To: "struts" <st...@jakarta.apache.org>
> Sent: Monday, December 01, 2003 1:28 PM
> Subject: forward vs redirect ?
>
>
> > Hi,
> >     I would like to receive comments on when to use forward and redirect
> in
> > action mapping. Do people use forward only. All the articles and
examples
> I
> > saw uses forward.
> >
> > Antony Paul.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Claire Wall <cl...@kurtosys.com>.
Hi Antony,

As a rule i always use just forward unless there is a specific need to
redirect to the page you're forwarding to.

For example, say i had an action which saved some details from a form to a
database. if the page is not redirected then if the user were to refresh the
page, the action would be called again and the data saved again. This
obviously i would not want the user to do so in this case i redirect the
page, so that if the user were to refresh the page, the save action would
not be called again.

The times when I would advise against using redirecting is when you're
storing objects on the request object in your action. If you redirect then
this information is lost. There are ways around this (like putting the
objects on the session rather than on the request object) but i try to cut
down on the amount of information being stored in the session.

There are probably some better defined uses of the redirect attribute and
when/where to use it, but the above is the general rules i go by, having run
into problems with both redirecting and not redirecting in the past.

hope this helps

Claire


----- Original Message -----
From: "Antony Paul" <an...@hotmail.com>
To: "struts" <st...@jakarta.apache.org>
Sent: Monday, December 01, 2003 1:28 PM
Subject: forward vs redirect ?


> Hi,
>     I would like to receive comments on when to use forward and redirect
in
> action mapping. Do people use forward only. All the articles and examples
I
> saw uses forward.
>
> Antony Paul.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: forward vs redirect ?

Posted by Hubert Rabago <ja...@yahoo.com>.
Anthony,

I use redirect to separate actions which do work and actions which display
information.
Let's say I have 2 pages/Action objects:  DisplayItem, ProcessBid.
The user sees an item he likes (say displayItem.do?id=123) and enters a bid.  The
bid will get submitted to processBid.do.  Once ProcessBid is finished, it will
redirect to displayItem.do?id=123 which will show the item and (at this point)
the updated bid amount.  

The advantages are these:
* My "process" action (and any other action which does processing) doesn't need
to chain the display action.
* The user can refresh the resulting page without me having to worry about any
form getting processed twice. (this is a real ebay problem, by the way, and kinda
sucks when you're entering a bid at the last minute and need up-to-the-second
updates)
* The url of the resulting page is preserved, allowing the user to bookmark it or
forward it if he so desired.  (Not too helpful for the auction example, but think
about "New Record" processing where you show a page which displays a newly
entered record.)

HTH,

Hubert

--- Antony Paul <an...@hotmail.com> wrote:
> Hi,
>     I would like to receive comments on when to use forward and redirect in
> action mapping. Do people use forward only. All the articles and examples I
> saw uses forward.
> 
> Antony Paul.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org