aspose file tools*
The moose likes Servlets and the fly likes Sharing a Static Object between Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sharing a Static Object between Servlets" Watch "Sharing a Static Object between Servlets" New topic
Author

Sharing a Static Object between Servlets

Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Hi,

I have a static hash table that i use to store user information in. I have two servlets running in tomcat. I populate the HashTable in one Servlet and read the HashTable in another servlet. Here is my web.xml file




I have a HashTable that stores the user information that is not part of any servlet but i nead both servlets to use it. If i just use
request.getSession().setAttribute and put the HashTable in the session will that work? But since the HashTable is Static i want the very same instance of it to be used by bothe servlets. How can i do that ?

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3092
    
  40

If that Hashtable is a public static member of a class, why do you need to set it as an attribute? You can simply access it with the class name.

I have a HashTable that stores the user information...

If that Hashtable is populated with user information (on session creation), you should set is as a session attribute, and should NOT use it as a static member anywhere.

Devaka

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

Ok so if one servlet puts something in a static hash table the other servlet can read from that ? Servlets are not running in their own application space.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

If the static member is declared public, the other servlet will be able to access it.

But that could be a rather poor design. If something should be shared among servlets, one servlet shouldn't "own it" unless there's a really good reason for it to do so.

Have you considered using a context listener to place the data in application scope?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

No i haven't. Could you please guide me as how this can achieved. How can we set up a context listener.

Kind Regards.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3092
    
  40

Have a look at this tutorial for how to set a servlet context listener. Using such listener, you can set your Hashmap object as an application-scoped attribute - which is commonly accessible for all of the Servlets. However, if your Hashmap is to be populated for each user on per-session basis, you should consider using a session listener.

Devaka
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sharing a Static Object between Servlets