• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing a Static Object between Servlets

 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4501
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64964
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4501
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic