File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts 1 vs Struts 2 - singleton vs non-singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 1 vs Struts 2 - singleton vs non-singleton" Watch "Struts 1 vs Struts 2 - singleton vs non-singleton" New topic
Author

Struts 1 vs Struts 2 - singleton vs non-singleton

Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 91
Hello,

I've read the following difference between Struts 1 and Struts 2:

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.

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.)


I know that a singleton class is a class that can have only one instance. But I still do not understand the true nature of the difference between these two versions of Struts when using singleton and non-singleton strategies. Could you please tell me more about this and give me an example?

Thank you.
Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 91
Can anyone help me?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

I'm not sure what you are looking for here. Struts 1 and 2 have different approaches to actions. Since Struts 1 only creates one instance of a particular action, the programmer must always keep thread safety in mind. For example, if one uses an instance variable in an action, it can create a race condition as multiple threads each set the variable to their own particular value.
Since Struts 2 creates an action instance for each request, this is one less thing the developer has to worry about.


[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 1 vs Struts 2 - singleton vs non-singleton