Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables shares the same value across sessions?

 
P Lavti
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They will share the same value, like in any java application.
 
P Lavti
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it so. then how can i store a session specific values in a helper class of a web-application?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P Lavti
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P Lavti
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic