This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Global Storage Of Values

 
Jo George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
How can we store anything globally in an application server for a specific user. For example if i login a user and get teh user's subject, how can I store the subject information in the session and later some other utility class use that subject information without needing to pass the request object or the session object to that utility class?
Thanks in advance
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the kind of thing people use a database for but there are other alternatives. For example you might have a Serializable object to hold that data and store it in the session or serialize it to a disk file with a file name based on the user's id. Serialization can be quite fast if the object is not too complex.
How "global" do you want this to be? Between web applications or all in one web application?
Bill
 
Jo George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the reply. But cant it be done in memory? For example by using a cache implementation or maybe a session bean(state)??
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the ServletContext for storing data accessible to the whole application.
In any servlet, you can call getServletContext(), and then from there you can use .set/getAttribute() just like the HttpSession object.
 
Jo George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks phil
But that will be accessible only from servlets. Any other non-servlet class wont be able to access it rite?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These non-servlet classes you want to have access - are they in the same JVM as the servlet engine? How do they get created/started? Do they just get used by one "web application"? Could you just pass the constructor a reference to the ServletContext?
Alternately there is a very nice free all java database that can be implemented as in-memory only if you like. Search for Hypersonic SQL.
Bill
 
Jo George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I wanted to store the user specific info from a servlet/jsp and then store it somewhere globally so that other classes can pick that value for that user session. For example consider the case when a user logs in and the user is authenticated using some authentication mechanism. Now the subject needs to be stored globally somewhere for that session so that another class that determines what his principals are. Weblogic does this by storing the subject in the user thread and then we can use weblogic.security.security.getUserSubject() to get that subject. I was trying to implement something on the same lines so that i can store the user specific data somewhere without the help of any application server.
Thanks
 
Milind Kolapkar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to define storage class as static so servlets and any other class in same JVM can access same object...
 
Jo George
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Milind
Static wont work because the life cycle of that class would be not "forever", unless u are enclosing it with a thread. For example lets suppose servlet A stores a value in the storage class but once the servlet has stored it and the global storage goes out of scope, it gets garbage collected.
Thanks
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these other processes that need to access the data not started by the server? Are you manually starting them outside of the web application? If the user-specific data is only needed in the processes that are ultimately started by some web application process, then shouldn't you just keep the data in the session and pass it along as a parameter?
Alternatively, could you just store the data you need in a file or a DB.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic