Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class variable safe in struts 2 action ?

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic