sessionMap.invalidate() not functioning when trying to logout

I am making a Struts2 application.

I am trying to logout, but I am getting java.lang.nullpointerexception

My method is:

  public String logout(){

    Map session = ActionContext.getContext().getSession();
    SessionMap sessionMap=(SessionMap)session;
    log.info("session:"+session);
    log.info("sessionmap:"+sessionMap);
    log.info("sessionMap.get()"+sessionMap.get("USROBJECT"));
    try{
    if(sessionMap!=null){

        sessionMap.remove("USROBJECT");
        sessionMap.invalidate();

    }
    }
    catch(Exception e){
        log.info("Exception is:"+e.toString());
    }


    return "LOGOUT";
}

I am getting the object when I am trying to print session , sessionMap and "USROBJECT" object, but when I am calling remove, I am getting an exception. Please help.

login method:

  public Map session = ActionContext.getContext().getSession();
  public SessionMap sessionMap;   
  public void setSession(Map session) {
    sessionMap=(SessionMap) session;
    }

  public String login(){
       us = new UserSession();
       us= getUserDetails();
       log.info("userName is:"+us.getUserName());
       log.info("password is:"+us.getPassword());
       us.setLastLoginTime(loginTime);

       sessionMap.put("USROBJECT", us);
       return "register";
    }

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" /> 
 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <package name="Hello" extends="struts-default">

   <action name="*LoginAction" method="{1}"
        class="com.pgnext.apps.action.LoginAction">




    <result name="register">/jsp/register.jsp</result> 
    <result name="error">/jsp/error.jsp</result>
    <result name="logout">/jsp/login.jsp</result> 
  </action>

  </package>
</struts>
  17:09:31,038  INFO LoginAction:171 - going to remove USROBJECT
  17:09:31,038  INFO LoginAction:183 - Exception is : java.lang.NullPointerException

Answers


You don't need to invalidate the session (a session is usually created when you land on a JSP), just remove your object from it:

private final static String USER_KEY = "USROBJECT";

public String logout(){
    Map<String,Object> session = ActionContext.getContext().getSession();
    if (session.containsKey(USER_KEY)) { 
        session.remove(USER_KEY);
    }
    return "LOGOUT";
}

Also consider


I think the problem you have with session invalidation that you are trying reuse the session object after the session is invalidated. I have explained it here.

If you want to access the struts session after you invalidated the servlet session you should update or renew the struts session.

On the JSP page you can check the object with s:if tag

<s:if test="#session.USROBJECT != null">
  ....
</s:if>

After session invalidation you should return a result of type redirect or redirectAction.

<result type="redirect" name="logout">/jsp/login.jsp</result> 

The login.jsp should not be a welcome-file listed in the web.xml.

And in login() action you didn't initialize a sessionMap.

   public String login(){
       Map session = ActionContext.getContext().getSession();
       SessionMap sessionMap=(SessionMap)session;
       us = new UserSession();  //don't need it
       UserSession us = getUserDetails();
       if (us != null) {
         log.info("userName is:"+us.getUserName());
         log.info("password is:"+us.getPassword());
         us.setLastLoginTime(loginTime);

         sessionMap.put("USROBJECT", us);
       }
       return "register";
    }


Need Your Help

How to add UITabBarController to OpenGL xCode 4.2 template?

objective-c uikit ios5 opengl-es-2.0

How to add UITabBarController to OpenGL xCode 4.2 template ? For some reasons, I have to mix UIKit project with OpenGL ES template.

How can I make a sequence with entered values by button in Java?

java user-interface listener actionlistener

I want write program who in t1(TextField) enter how many numbers user want enter and confirm by b1(Button). In t2(TextField) user give a first value and enter it by b2(Button), in next time user gi...