Search code examples
javasessioninstancestruts

How Action class object is available even after session is invaliated?


I'm using Struts 1.2 and I need a clarification about the Action class.

My Action class has an instance variable count and an Instance Initialization Block with an SOP statement which will print the incremented value of count (incremented by 1). Then I configured this Action class for a screen in my application. Now when ever I hit this screen, my Action class will be executed.

I accessed this screen form two different browsers and I found that the SOP inside the Instance Initialization Block executed only once which concludes that only on object is created for both the users.

Now when I signed out from both the browsers and I again signed in to access the same screen the SOP inside the Instance Initialization Block wasn't executed (so the Action class object is still available). But when I removed my project from the running server and published again, the SOP was executed.

So I think the Action class object is not related with session and if it is not, then where it is getting stored so that even after session gets invalidated Action class object is available?

Sample Action class code :-

public class SampleAction  extends DispatchAction {
   private int count;

   {
         System.out.println(++count + " object(s) created");
   }
   /* other methods */
}

Solution

  • I think the Action class object is not related with session and if it is not, then where it is getting stored so that even after session gets invalidated Action class object is available ?

    Yes, ActionServlet and Action class objects are created and maintained by the servlet container and they are singleton objects (by default) i.e., only one instance of those classes are maintained for the whole web application, unlike your FormBean objects.

    So ActionServlet and Action class objects live in the container once they are loaded (either during startup or when the first request hits the container) till the application gets destroyed irrespective of the user sessions.

    Also, note that Action class objects are completely different from FormBean class objects (like ProductFormBean, EmployeeFormBean, etc...) i.e., FormBean objects are tied to each JSP page and they will be instantiated (one object will be created) and populated (with JSP fields data) whenver request comes from the user.