Tutorial :Servlet context serializable attribute persistence



Question:

I set a servlet context attribute, which is an object of Serializable class. Does it persist after application server restart as session attributes do?


Solution:1

Attributes in servlet contexts are not automatically persisted. If you want to persist them you will need to do it yourself.

I don't think persistence of servlet context attributes would be a good idea. There is a non-zero chance that the servlet container will terminate abruptly without giving your webapp a chance to persist its attributes. Your webapp would need to deal with this possibility on restart.


Solution:2

This is not required/specified by the Servlet API. Technically, this depends on the servletcontainer in question. As far as I know, no one does that. You'd like to consult the documentation of the servletcontainer in question. If it doesn't support that, then you need to persist and revive it yourself with help of a ServletContextListener and ObjectOutputStream and ObjectInputStream.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »