I have a class which extends the HttpServlet class. I know that Java it’s multithreaded and different threads (read: HTTP requests) can make use of the same instance. I want to instantiate an Object on the first request and use it until the end of session.
How should I proceed?
For example : I want to open a file on the first request and close it at the end of session.
You could either just instantiate it yourself in one of the servlet’s
doXxx()methods depending on its presence in the sessionOr you could instantiate it yourself in a
HttpSessionListenerEither way, the
SomeObjectitself could implementHttpSessionBindingListenerso that you can perform some logic during binding and unbinding to the session.