Billworth Vandory
Billworth Vandory

Reputation: 5053

how to check if a sessionId is valid in a servlet (java)

I am maintaining a map of sessionid(s) and HttpSession Objects in my web app. I use an HttpSessionListener to populate or remove a session from the map. When my web server crashes and goes down and comes back up, i need a way to check if the sessionid being submitted is valid or not. Obviously, when the app comes back online, the Map is empty, therefore all session id's are invalid, but I just want some way to check the incoming sessionid, if possible, at all..

thanks

Upvotes: 2

Views: 5082

Answers (3)

Mike Baranczak
Mike Baranczak

Reputation: 8374

Why on earth are you maintaining the sessions yourself? The servlet container is supposed to do that for you. I haven't seen your code, maybe you have a good reason for doing what you're doing, but I get the feeling that you just don't understand the servlet API, and are trying to re-implement it yourself.

Also, you might be interested in this: Tomcat has a feature where it persists the sessions, which means that the session state survives a server restart. (Turn this off when updating the application, since there might be mismatches between different versions of classes.)

Upvotes: 1

Adeel Ansari
Adeel Ansari

Reputation: 39887

You can keep those in DB in some table. This way it will not suffer by the crash. And by the way, servers serializes the session to the disk, if you want them too.

Upvotes: 3

Suresh Kumar
Suresh Kumar

Reputation: 11767

One suggestion is to serialize your Map to disk and reload when the Web server starts up. The strategy to backup your Map to disk depends on your requirement, you could write to disk each time you add/update the map or in regular intervals. It is fairly easy to write your session data to disk, this is something that is often done for achieving redundancy and load balancing scenarios.

Upvotes: 1

Related Questions