aspose file tools*
The moose likes Servlets and the fly likes Static variables shares the same value across sessions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Static variables shares the same value across sessions?" Watch "Static variables shares the same value across sessions?" New topic
Author

Static variables shares the same value across sessions?

P Lavti
Ranch Hand

Joined: Mar 27, 2007
Posts: 65
Hi,

I am developing a web application. And I want to store a value, which is unique for one session, in a helper Java class.

I can't store it in the session, as I need to access it from helper classes, not from servlet or JSP.

If I save it in a static variable, and suppose two user are accessing the web application with different session id, will they share the same value as it is a static variable? If no then why?

Thanks!


-P Lavti<br />SCJP 5.0 (88%)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

They will share the same value, like in any java application.


[My Blog]
All roads lead to JavaRanch
P Lavti
Ranch Hand

Joined: Mar 27, 2007
Posts: 65
if it so. then how can i store a session specific values in a helper class of a web-application?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Lavti,

It not a good idea to use a static variable to maintain different value per session.

Its good idea to store it in session. and when ever required to other classes(other than servlets or JSP) you have to take it from session, pass the value to its constructor.
[ April 23, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
If you have to instantiate the helper class then you might want to pass the session object to its constructor.But mostly helper classes contain static methods , in that case your methods might have one extra session variable.

One disadvantage of this approach : The helper class will be tightly coupled to the container.


Rahul Bhattacharjee
LinkedIn - Blog
P Lavti
Ranch Hand

Joined: Mar 27, 2007
Posts: 65
I agree that I cann't keep the session related values in static variable. But I want to avoid it storing into session as well.

Since I have to use this value several times, and if I store it into the session, everytime I need to create the instanse of the helper class, paasing the value in the constructor.

By any chance is there any other alternative for this so that if I set the value once, I can use that value in helper classes as and when required?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by P Lavti:
I agree that I cann't keep the session related values in static variable. But I want to avoid it storing into session as well.

Since I have to use this value several times, and if I store it into the session, everytime I need to create the instanse of the helper class, paasing the value in the constructor.

By any chance is there any other alternative for this so that if I set the value once, I can use that value in helper classes as and when required?


If you have anything specific to a user session , like for example if you have the userName specific to a session then you can do the following.

One singon of the user you can store that object in application context and the key would be the username , so whenever you need that object for that particular user then just query the application with the username as the key.During the logoff process you have to remove the username from the application context.

I really do not think that it would be a good way to do this.


Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Rahul,

Thats not all suggestable.
Context will be the same for all users using the webapp.

Hi Lavti,

Whats your requirement to go for a Helper class ?
How you want to design your Helper class for your requirement ?

[ April 23, 2007: Message edited by: Srinivasan thoyyeti ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Srinivasan thoyyeti:


Thats not all suggestable.
Context will be the same for all users using the webapp.


Yes , thats true context will be same for all the users.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This is exactly what sessions are for.
If you can access the helper object from your web objects (servlets/jsps) then you can pass references to it.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
P Lavti
Ranch Hand

Joined: Mar 27, 2007
Posts: 65
My web-application is already developed and will be available to customers very soon. And suddenly we had a new requirement of storing user's session related values.

I was knowing that it can be done by storing the values in the session, but that will require changes in the jsp pages, so I was looking for an alternative solution.

But it seems I can't have any alternate way to do the same and have to go ahead with modifying all the jsp pages

Thanks everybody for your support!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static variables shares the same value across sessions?