*
The moose likes Struts and the fly likes Do I need to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Do I need to "add" encodeURL for "action" tag in this case ?" Watch "Do I need to "add" encodeURL for "action" tag in this case ?" New topic
Author

Do I need to "add" encodeURL for "action" tag in this case ?

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
In my struts-config.xml, I set

<action path="/action" input="/input.jsp"
scope="request">
<forward name="success" path="/success.jsp" />

So, the scopeis set as "request" here, but in my Action class I use "session" and store objects in the session. So, in my input.jsp I will do

input.jsp:
************************
<html:form action="<%= response.encodeURL("/action") %>" >
....
</html:form>
*************************

Now my questions are --

Do I need to do anything for the struts-config.xml's "path=/action" ?
Do I need to do anything for the struts-config.xml's "forward path="/success.jsp" ?

Or will struts automatically take care of it ? Please be aware I use "scope=request" for each <action> definition, so do I need to do anything explicitly in the struts-config.xml or any other places besides what I mentiioned for the <html:form action=..> ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Struts performs encodeURL automatically. You don't need to include it in the action attribute of html:form, or anywhere else. As long as you use struts tags and forwards, the encodeURL is being done for you.


Merrill
Consultant, Sima Solutions
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
Originally posted by Merrill Higginson:
Struts performs encodeURL automatically. You don't need to include it in the action attribute of html:form, or anywhere else. As long as you use struts tags and forwards, the encodeURL is being done for you.


Thanks. But I am just curious -- There must a reason why struts chooses to use "encodeURL" and usually that's when it realizes the "session" is being used (otherwise, no need). Is my guess correct ? If so, then how does struts know "session" is used ? In my case, I do NOT set scope=session, instead I set scope=request. So, how can struts know I am using session ? Is it because it finds I use session in Action class ?

I am just trying to understand better about the mechanism. Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The way that encodeURL is handled is dependent on your application server. It is only used if the App server is configured to use URL rewriting to keep track of sessions. Otherwise, it is ignored. The most common way to keep track of session is with cookies. The application server puts a cookie on the user's browser and retrieves it every time a request is made. It uses that cookie to associate the user with a session.

The trouble with cookies, however, is that they can be turned off by individual users, making it impossible to track the sesion. So, if you want to be sure to track sessions even for users who don't accept cookies, configure your app server to use url Rewriting. With url rewriting, every time you call encodeURL, the app server will append a session ID as a parameter to any link. It will then look for that session id when the user communicates with the server.

I hope this answers your question.
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
Originally posted by Merrill Higginson:
The way that encodeURL is handled is dependent on your application server. It is only used if the App server is configured to use URL rewriting to keep track of sessions. Otherwise, it is ignored. The most common way to keep track of session is with cookies. The application server puts a cookie on the user's browser and retrieves it every time a request is made. It uses that cookie to associate the user with a session.

The trouble with cookies, however, is that they can be turned off by individual users, making it impossible to track the sesion. So, if you want to be sure to track sessions even for users who don't accept cookies, configure your app server to use url Rewriting. With url rewriting, every time you call encodeURL, the app server will append a session ID as a parameter to any link. It will then look for that session id when the user communicates with the server.

I hope this answers your question.


Thanks Merill, but I was not concerned about "how encodeURL" works. I was curious about "Under what condition will the struts use encodeURL 'automatically' ?"

You said, "encodeURL is performed by struts automatically".

1. If we do NOT use session anywhere in our Action code, and in struts-config.xml every "scope" is set as "request". Then will "encodeURL" be applied automatically as well ? I doubt.

2. If I set "scope=request", but in Action class I use session myself (of course, server 'creates' for me). will encodeURL be applied by struts automatically ?

3. if I set "scope=session" in struts-config.xml, then I am sure encodeURL will be applied because struts-config.xml is the file it reads.

My point is there must be a way to "tell" struts that you are using "session" so that it can apply "encodeURL" for you. Right ?

By the way, I added a encodeURL to the <html:form action=".."> and it still works, so it doesn't hurt though it may be unnecessary.
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
Now my guess is struts *always* uses "encodeURL" no matter you use "session" or not. Is that right ? A
 
Don't get me started about those stupid light bulbs.
 
subject: Do I need to "add" encodeURL for "action" tag in this case ?
 
Similar Threads
How to use only Server Side Validation
ActionForm not populated/is null after mapping
how to modify struts-config.xml to migrate struts2?
How to customize mapping in struts-config.xml ?
Issue with Struts 2 Dispatcher Result