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

Mutiple instances of request handlers

Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
I posted this question in the Struts forum, but nobody responded. Hoping for some replies in the Servlet forum, as it is much more active.

In essence the Q is that why do we have or why is it recommended to have only one instance of a request handler class ? What is the impact of having a new class to handle each request
Hi,
As per the the Struts documentation for an Action Class, only one instance of the class is created and so, the design of the class should ensure that it is thread-safe.
My Question is what will happen if I change the struts source code, so that the action servlet does not look for an existing instance to handle the incoming request, but creates a new instance for each request? What can/cannot go wrong if this the case? And what can be the performance implications in this scenario?
Best Regards,
Anupreet Arora
PS: I am pasting the section from the Struts documentation which refers to the issue, for the covenience of the readers
-----------------------------------------------------------
An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (RequestProcessor) will select an appropriate Action for each request, create an instance (if necessary), and call the execute method.
Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:
Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary
------------------------------------------------------------
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
My Question is what will happen if I change the struts source code, so that the action servlet does not look for an existing instance to handle the incoming request, but creates a new instance for each request? What can/cannot go wrong if this the case? And what can be the performance implications in this scenario?

There would be some performance hit for the object creation and subsequent GC, but newer JVMs are much more efficient at this. On the plus side, your code for a "one instance per request" design could be simpler and possibly faster.
I recently ran into this rather nice discussion about performance for this style of programming that I heartily recommend.
Bill


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mutiple instances of request handlers
 
Similar Threads
Struts Doubt
What are the differences btw MVC and Front Controller design patterns?
class variable safe in struts 2 action ?
Multiple instances of an Action class
Cookie problem - cookie shared between 2 external seperate networks