aspose file tools*
The moose likes Struts and the fly likes class variable safe in struts 2 action ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "class variable safe in struts 2 action ?" Watch "class variable safe in struts 2 action ?" New topic
Author

class variable safe in struts 2 action ?

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
I have a JSP form which has fields for "product name", "price", etc. When clicking submit button of this page it invokes ProductAction class which looks like



Question --- Is it proper to define "productName" as class variable ? What if multiple users access this JSP page , then each of their request will come to TestAction, will this "productName" be safe in the sense it does not mess up the input from different users ?

Thanks in advance.
Orton K Randy
Ranch Hand

Joined: Aug 12, 2011
Posts: 41

Threading model of Struts1:
Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. The singleton strategy places restrictions on what can be done with Struts 1 Actions and requires extra care to develop. Action resources must be thread-safe or synchronized.


and that of Struts2:
Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues. (In practice, servlet containers generate many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection.)


Like it's been pointed out, Struts 2 actions are NOT singletons, they're instantiated for every user request. So you don't have to worry about thread-safety, your instance(and of course class) variables are inherently thread-safe.


Coderanch, best ever forum on the net. Hands down.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class variable safe in struts 2 action ?