Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset session in jsp without invalidating

Tags:

java

jsp

session

I want to reset the session in a JSP page without invalidating it. The reason for this is, the user might have an already opened page using the session and invalidating it would throw a NullPointerException. Since a fresh session is already been catched, I don't want to add an additional catch-phrase. The goal is to clean all Attributes.

I am looking for something like:

session = new HttpSession(); //this does obviously not work

an alternative would be (?)

while(session.getAttributeNames().hasMoreElements()){
    session.removeAttribute(session.getAttributeNames().nextElement());
}    //cleans session?

But I am not sure, if this deletes some necessary session-attributes like login-data.

Is there a nice way to do this?

Edit: The best solution would be, each time you open a certain page you create a new session for that window/tab until you close the tab or revisit that page in this tab. This question occurred by the attempt to solve this problem.

like image 759
ctst Avatar asked Feb 02 '16 14:02

ctst


People also ask

What is session invalidate in JSP?

You can call invalidate to discard an entire session. Just remember that doing so causes all of that user's session data to be lost, not just the session data that your servlet or JSP page created. So, all the servlets and JSP pages in a Web application have to agree on the cases for which invalidate may be called.

How can I delete session in JSP?

Remove a particular attribute − You can call the public void removeAttribute(String name) method to delete the value associated with the particular key. Delete the whole session − You can call the public void invalidate() method to discard an entire session.

How check session is valid or not in JSP?

HttpSession session = request. getSession(false); if(session != null){...} returns null if the attribute does not exist... so your NullPointerException occurs because you try to call equals on null.


2 Answers

Yes there's a nice way to do this. Create a class that wraps the HttpSession object and implement a Map interface. In the implementation you can manipulate session attributes without invalidation of the session. Similar like the implemented code for SessionMap in the Struts2 framework. You can read docs page for SessionMap here.

A simple implementation of the Map interface to handle a collection of HTTP session attributes. The entrySet() method enumerates over all session attributes and creates a Set of entries. Note, this will occur lazily - only when the entry set is asked for.

Similar implementation of SessionMap you can find in JSF. The docs page you can find here.

public class SessionMap extends
    java.util.AbstractMap<java.lang.String,V>

See Also: javax.faces.context.ExternalContext#getSessionMap()

If you use any of these frameworks, you can use or override the clear() method to get what you want.

like image 87
Roman C Avatar answered Oct 14 '22 22:10

Roman C


The HttpSession API clearly does not provide such feature.

The above loop might be written a bit nicer using the Java5 iterator idiom:

for (final String an : session.getAttributeNames()) {
    session.removeAttribute(an);
}

But that requires the Enumeration to be iterable.

What I am wondering: You want to clear all session data, but still keep the session so that some other control run, which seems to be using the session, does not crash. Does this really work? The second control run will not find any session data, as you have completely cleared out the session.

I believe one of those is true:

  1. your session clean-up removes data needed by the second control-run
  2. it would be better to invalidate the session and enable the control-run to deal with an invalidated session.

The HttpSession documentation clearly states that any entity using the session must be able to deal with the fact that a session gets lost or never existed in the first place, so point 2 should be implemeneted anyways.

In the sense of extendiblity (what happens if there is data from other processes in your session? Your loop would clear them out as well) I suggest instead of clearing the whole session you keep track of the session values put by your control-run and clear out those explicit values only.

like image 44
penguineer Avatar answered Oct 14 '22 22:10

penguineer