permaculture playing cards*
The moose likes Servlets and the fly likes servletcontext-attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "servletcontext-attribute" Watch "servletcontext-attribute" New topic
Author

servletcontext-attribute

sri jaisi
Greenhorn

Joined: Sep 02, 2006
Posts: 23
we use servlet context to share common parameters between all the servlets in our application;we store them in web.xml(contextparam).we retrive them through getInitParameter;but servletcontext interface also has setAttribute;getattribute methods;where we have to use them and how
thaknz
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
but servletcontext interface also has setAttribute;getattribute methods;where we have to use them and how

You can use those methods to share data (which might be the result of a calculation based on the user's answer on a form) between all the servlets in your application. However, the data isn't thread safe, so another request from another client can set those variables to something else before the servlets handling the first request have a chance to read them.
[ October 14, 2006: Message edited by: sven studde ]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

From what I believe, the parameters in the xml file are read only. The context parameters added and removed at runtime are read/write.

Be careful with the ServletContext. It is not necessarily workload managed across JVMs in a clustered environment, so not only is it not threadsafe, but contexts can also get out of sync. A centralized database is so much nicer to use in a workload managed environment.

Cheers!

-Cameron McKenzie
 
Consider Paul's rocket mass heater.
 
subject: servletcontext-attribute