aspose file tools*
The moose likes Struts and the fly likes Action.getLocale( HttpServletRequest) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action.getLocale( HttpServletRequest)" Watch "Action.getLocale( HttpServletRequest)" New topic
Author

Action.getLocale( HttpServletRequest)

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Any idea why we need the request object in Action.getLocale(HttpServletRequest)? Isn't an Action object already associated with a request object implicitly?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Struts uses the getLocale() method of the HttpServletRequest object to get its locale.

Isn't an Action object already associated with a request object implicitly?


The request object is passed into the Action's execute method, but that doesn't mean that the Action class is implicitly associated with the request. Any time the request object needs to be used by any method other than execute, it still has to be passed as a parameter.


Merrill
Consultant, Sima Solutions
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
... which gives you pretty much control over it (same goes for response, mapping and form)...

even though i have to admit that i never used it for anything which couldn't have been achieved with getters like:



honestly dont know why struts makes us passing these object-refs around all day long... :-)

jan
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The reason having a "myAction.getRequest()" method isn't practical is that the Action class is used in a threaded environment and must be coded as thread-safe. This means that an Action class cannot have instance variables that aren't the same for every user.

For example, an Action could have an instance variable referring to a logger because that is the same for every user. The HttpServletRequest object is definitely not the same for every user, and would have disastrous effects if put in an instance variable.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
i thought each request (= thread) gets its own action-instance? am i wrong about this is?

jan
[ October 06, 2006: Message edited by: Jan Groth ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Actually, yes, you are wrong about this. There is only one instance of each Action Class per container. It's very much like a Servlet in that multiple threads all go through the same instance of the Class. That's why it's very important that it be coded as thread-safe.

The ActionForm, on the other hand is not multi-threaded. A new instance gets created for each session or request depending on the scope specified in the action mapping. No need to worry about this object being thread-safe.
[ October 06, 2006: Message edited by: Merrill Higginson ]
 
jQuery in Action, 2nd edition
 
subject: Action.getLocale( HttpServletRequest)