I want to invalidate few particular sessions that i saved using "httpsession".Is it possible to invalidate selected sessions and keep the remaining sessions alive?
-
what do you mean by selected sessions? – Prasad Kharkar Aug 27 '13 at 07:51
-
Suppose a client has name, age and number attributes.Suppose I have saved these in session as setsession("name",name),setsession("age",age),setsession("number",number). So how do I invalidate only the name session and keep the rest alive ? – chao dee Jun 29 '14 at 10:28
5 Answers
Yes, just call HttpSession.invalidate()
or HttpSession.logout()
(Servlet 3.0).

- 305,947
- 44
- 307
- 483
-
-
-
it is useful when you want to invalidate whole Session. but suppose i want to remove particular session or variable from session than what. If you have better way than session.removeAttribute("xyz") than explain. but remember i want just remove variable from session not whole session. – Karan May 03 '15 at 16:07
there are two way to invalidate session in servlet/jsp
1--session.invalidate();
2--session.removeAttribute("xyz");
to invalidate particular session we use 2nd option instead of xyz you put any session name there

- 557
- 6
- 17
i think this is best way to invalidate particular session not completely
session.invalidate();
and
session.removeAttribute("username");

- 41
- 2
I have written a method which does invalidate session with given sessionID:
public void expireSessionWithId(String sessionID)
{
try {
MBeanServer server = java.lang.management.ManagementFactory.getPlatformMBeanServer();
ObjectName objectName=new ObjectName("jboss.web:type=Manager,path=/test,host=default-host");
// declare signature of the parameter
String[] sig = { "java.lang.String"};
// declare parameter
Object[] opArgs1 = { sessionID };
// call the method
String value = (String) server.invoke(objectName, "expireSession",
opArgs1, sig);
System.out.println(value);
} catch (MalformedObjectNameException e) {
//handle the exception
} catch (InstanceNotFoundException e) {
//handle the exception
} catch (ReflectionException e) {
//handle the exception
} catch (MBeanException e) {
//handle the exception
}
}
I am working on JBoss-7.1.1.Final. My application is called "test", hence the context root "/test". You should create objectName with name of your application.

- 1,625
- 4
- 17
- 35
-
Your answer is JBoss-specific, and JBoss is not mentioned in the question. -1 – user207421 Aug 28 '13 at 08:43
'EJP's' solution works perfect for current session. But if You want to invalidate any session (based on the specified JSESSIONID) here is the answer: How to invalidate selected session programmatically?
-
Can You tell me how to do it? I had the same problem month (or two) ago. So the question is: How can I invalidate session with selected id (i.e 'xyz') from different one session. – pWoz Aug 27 '13 at 11:48
-
Get the session, as shown in your link, and then call one of the APIs mentioned in my answer. – user207421 Aug 28 '13 at 08:42