Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Targetting specific Objects in session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Targetting specific Objects in session" Watch "Targetting specific Objects in session" New topic
Author

Targetting specific Objects in session

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

i have a java bean that i store in each user's session. Each jsp page contains the following line..



The User object contains information about the user and a list IDs of products they can view.

I have a Servlet which add and removes these lists of products from users. The problem i have is if a user's account is updated, i would like this user's bean to be refreshed.

I cant think of an easier way to do this. How do i access each User object for each user in all sessions so that i can invalidate the ones i want?
[ May 23, 2006: Message edited by: O. Ziggy ]
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
I don't really understand the problem here.

The JSPs will always get the current data from the User object in the session. It doesn't cache this data. What do you mean, when you say you want to refresh User object.

Is your servlet(which updates the User object) not being called every time the user data is updated?

The problem i have is if a user's account is updated, i would like this user's bean to be refreshed.

Any object is session is latest one. What do you mean by refreshing?

How do i access each User object for each user in all sessions so that i can invalidate the ones i want?

If you want to share data across sessions, you either need to put it in ServletContext or you have to put it in some external resource like database.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I'm assuming the servlet is run by a different Session than the logged in user. Something like an Admin Servlet right?

If so, there are several ways to handle this.

1) have the user refresh from your data store on each request

2) create an HttpSessionBindingListener and place it in the ServletContext as each User is created, get the sessionId and place it in a map. The servlet can then get the map and refresh the user. If a user is unbound, remove it from the map.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Basicly when the user logs on, his or her details are stored in the "User" bean. This details come from the database and stored in this bean which is available to the user for the scope of the session.

Every request the user makes uses this bean to check what products the user can view. This bean is created when the user logs in. The only way to refresh it is to log out the user.

If this user's profile is updated, the change is made to the database. If the user is still logged on, the bean in his session will still contain the old profile information.

I am trying to refresh or destroy all User beans on all sessions if a user 's profile is updated. This will force the beans to be re-created with the updated information.

Hope that made it clearer.
[ May 24, 2006: Message edited by: O. Ziggy ]
Ravindra Rawat
Ranch Hand

Joined: Dec 09, 2004
Posts: 34
Pusing invalidation message to all session variables would be difficult to achieve, but you can have some kind of check in your user bean which checks for last update date in your web application either the database or some variable in application wide context.

- Ravindra
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I got it to work.

Basicly each "User" bean object has a field for the time it is created. Whenever a user profile is updated i use a global date which is set to the time the update took place.

Everytime any user makes a request i compare the time their bean was created with the time the the last update took place. If the bean was created before the update then the session is invalidated.

Cheers..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Targetting specific Objects in session
 
Similar Threads
How to create a session-scoped user profile bean at login?
EJB Questions
Accidental session hijacking...maybe caused by Session Bean?
Help...Static items in session beans
Is session is a strange thing?