• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sharing a Static Object between Servlets

 
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic