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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
chao dee
  • 21
  • 1
  • 8
  • 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 Answers5

2

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • wouldn't it invalidate all the sessions of the related client ? – chao dee Jun 29 '14 at 10:25
  • @chaodee No, it will invalidate the session you call it on. – user207421 May 03 '15 at 11:31
  • 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
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

Karan
  • 557
  • 6
  • 17
1

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

session.invalidate();

and

session.removeAttribute("username");
Vicks
  • 41
  • 2
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.

fascynacja
  • 1,625
  • 4
  • 17
  • 35
-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?

Community
  • 1
  • 1
pWoz
  • 1,649
  • 3
  • 20
  • 30
  • 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