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

Global Storage Of Values

Jo George
Greenhorn

Joined: Apr 17, 2003
Posts: 6
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

Joined: Mar 22, 2000
Posts: 12825
    
    5
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

Joined: Apr 17, 2003
Posts: 6
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

Joined: Feb 15, 2003
Posts: 251
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

Joined: Apr 17, 2003
Posts: 6
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

Joined: Mar 22, 2000
Posts: 12825
    
    5
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

Joined: Apr 17, 2003
Posts: 6
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

Joined: Apr 15, 2003
Posts: 2
You need to define storage class as static so servlets and any other class in same JVM can access same object...


M I L I N D
Jo George
Greenhorn

Joined: Apr 17, 2003
Posts: 6
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

Joined: Feb 15, 2003
Posts: 251
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.
 
jQuery in Action, 2nd edition
 
subject: Global Storage Of Values