0

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?

2
  • what do you mean by selected sessions? Commented 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 ? Commented Jun 29, 2014 at 10:28

5 Answers 5

2

Yes, just call HttpSession.invalidate() or HttpSession.logout() (Servlet 3.0).

Sign up to request clarification or add additional context in comments.

3 Comments

wouldn't it invalidate all the sessions of the related client ?
@chaodee No, it will invalidate the session you call it on.
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.
1

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

(2) is nonsense. The attributes of a session are not sessions.
1

i think this is best way to invalidate particular session not completely

session.invalidate(); 

and

session.removeAttribute("username"); 

Comments

0

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

Your answer is JBoss-specific, and JBoss is not mentioned in the question. -1
-1

'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?

2 Comments

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.
Get the session, as shown in your link, and then call one of the APIs mentioned in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.