Search code examples
javaobjectwebpojo

What happens to plain old Java objects when used by a web application by multiple users?


I am trying to create a simple service-like application. I am using the browser as the entry point, servlet as the receiver of the request and a couple of helper java classes. For example, I have user A go use the application at the same time as user B.

Now my question is, what happens to the helper classes when multiple users access the service? Do they get instantiated every time a user uses the application? 5 users, do they (helper classes) get instantiated 5 times, or for all the users, there is only one copy of everything?


Solution

  • That depends on when and where you instantiate those POJOs. If you are instantiating them on your doGet or doPost then you create new instances of those objects on every request(and discard them after the request has been responded to). Now if you are 'lazy instantiating' them onto a longer memory scope(session, etc) then you retain them longer(e.g. for session scope, throughout the life time of the session).