• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext

 
George Pavamani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a question regarding the ServletContext.
Q) What happens to the attributes bound to the ServletContext of a web-Application( Using setAttribute()method ) when the entire context is reloaded or when the Servlet Container is restarted???
Q)If the attributes bound to the ServletContext are lost because of the reloading, is there a way we can save the contents of the ServletContext before the loss of data??
Any help in this regard is greatly appreciated.
Thanks
George
 
nutan prakash
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the attributes that are set to the ServletContext are lost when the Servlet Container restarts/reloads.
If you want to save the contents of the attributes that were set to ServletContext you can write a ServletContextListener Class which does the job of storing the data in a persistent state (DB or Static files). The Listener is invoked when the ServletContext is destroyed. This happens when the Servlet Container is restarted or reloaded.
Regards,
Nutan
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this can happen due to one or more reasons........lets examine the cause first then the effect.
1. An "ADMIN" shuts down the server.
2. Server crashes.
3. Power loss.
While the ServletContextListener approach works for the first case, I don't think it works in the later two cases. So its always the responsibility of the application to maintain its state as needed. I am sure this is mentioned in the Spec., in one form or the other.
Anyone wants to volunteer to quote the Spec. if something is said re this?
In the last two cases, the application behaves like any other Java program, right?
My two cents........
- satya
 
George Pavamani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Satya and Nutan.
Is it possible to write a servlet/Java class that would automatically store the contents of the ServeltContext to a external storage from time to time, so that we can prevent loss of data during a powerloss or server crash.
Thanks again
George
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to write a servlet/Java class that would automatically store
Yes, it is possible.
- satya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic