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– Prasad Kharkar2013-08-27 07:51:05 +00:00Commented Aug 27, 2013 at 7: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– chao dee2014-06-29 10:28:24 +00:00Commented Jun 29, 2014 at 10:28
5 Answers
Yes, just call HttpSession.invalidate() or HttpSession.logout() (Servlet 3.0).
3 Comments
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
1 Comment
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 Comment
'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?