java.lang.IllegalStateException : getAttributeNames: Session already invalidated

标签 java struts

出现错误:

java.lang.IllegalStateException: getAttributeNames: Session already invalidated.After logout from parent window.

LogOutUserAction.java

public String execute() throws Exception { 
          System.out.println("inside :: LogOutUserAction------");
         //HttpServletRequest request = null;
          HttpServletRequest request = ServletActionContext.getRequest();
         HttpSession session =request.getSession(true);
         session.removeAttribute("loggedInUser");
        request.getSession(false).invalidate();  
      session=null;  
      return "logout";
      }

登录拦截器

public String intercept(ActionInvocation invocation) throws Exception {

        final ActionContext context = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) context
                .get(HTTP_REQUEST);     

        HttpServletResponse response = (HttpServletResponse) context
        .get(HTTP_RESPONSE);

        HttpSession session = request.getSession(true);
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
       /*user logged out from the parent window then set the message
        for click on the popup window*/
        if(session == null){
            System.out.println("set the attribute");
               request.setAttribute("SessionExpired","Your have already logged out");   
             }  

        Object user = session.getAttribute(USER_HANDLE);
        String loginOut = request.getParameter(LOGIN_OUT);  
        System.out.println("loginOut---->"+loginOut);
        if (user == null) {
            // The user has not logged in yet.
            System.out.println(" inside if ");
            // Is the user attempting to log in right now?
            String loginAttempt = request.getParameter(LOGIN_ATTEMPT);  
            /* The user is attempting to log in. */
            if (!StringUtils.isBlank(loginAttempt)) {               
                return invocation.invoke();
            }
            return "login";
        } else {            
            return invocation.invoke();
        }




    }

登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
  if (request.getAttribute("SessionExpired")!= null)
    {
      System.out.println("After Session invalid");


     }
%>



    <html>
    <head>
    <title>Login</title>
    <sx:head cache="true"/>
    <script type="text/javascript">

    </script>
    <script type="text/javascript" language="javascript" src="js/login.js"></script>
    <style type="text/css">
    .style1 {font-family: Verdana, Arial, Helvetica, sans-serif}
    .style2 {
        color: #000099
    }
    </style>
    </head>
    <body bgcolor="#CODFFD" background="<%request.getContextPath();%>images/watermark_new.jpg">
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <div align="center"><span class="style1">USER LOGIN</span>
        <br>
        <br>
        <br>
        <s:form action="checkUserLogin.action" validate="true" name = "loginForm">    
       <s:hidden name="loginAttempt" value="%{'1'}" />
       <table border="0" cellspacing="1" cellpadding="0" width="350" >
           <tr>
           <td align="center">
                <b>Login ID</b>
           </td>
             <td>
               <table>
                <s:textfield id="id" name="loginId" value=""   />
                </table>
              </td>
         </tr>
         <tr>   
           <td align="center">
                <b>Password</b>
           </td>

            <td> 
              <table>   
                <s:password id="password" name="loginPassword"  value=""  showPassword="true"/>
              </table>
            </td>
        </tr>
        <tr><td colspan="2" align="center"> 
              <table>   
                <s:submit value="Login" onclick = "return getRoleList()"/>
              </table>
            </td>
         </tr>
         <tr>       
            <td colspan="2" align="center">
              <table>  
             <s:a href="changePasswordScreen.action" >Change Password</s:a>
             </table>
            </td>
          </tr>
        </table>
       </s:form>

    </div>
    <br>
    <br>
    <br>


    </body>

    </html>

需要在弹出窗口上显示消息(当我单击任何链接时),正如我在从父窗口注销后在登录jsp中提到的那样。

最佳答案

拦截器可以在调用 Action 之前和之后执行代码。你的什么时候被调用?我想象发生的事情是这样的:

  • 在 LogOutUserAction.java 中,您使 session 无效
  • 然后执行LoginInterceptor.intercept。这会尝试从您已失效的 session 对象中获取一个属性,因此会出现 IllegalStateException。

为什么要使 session 无效?您的 LoginInterceptor 使用 USER_HANDLE 属性的存在来指示用户是否已登录。我会在您的 LogOutUserAction 中删除此属性,但不要使 session 无效。

关于java.lang.IllegalStateException : getAttributeNames: Session already invalidated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10618138/

相关文章:

java - Prowide-Core Library - 无法在 MT564 swift 消息中获取序列 E 标签

java - Spring 在 MongoDB 中将 Date 保存为 String - 无法从 String 类型转换为 Date 类型

javascript - Jquery 无法与 Internet Explorer 一起正常工作

java - 建议从 struts1 → (struts2, spring) 的进阶路径

java - Struts 2 Eclipse 类未找到错误

java - 使用 onPostExecute 设置全局数组值时出现问题

java - 如何从struts中的java Action 调用url?

java - 显示 struts 验证消息

java - 错误 : constraint for relation already exists with hibernate 4. 3.1

struts html :image vs html:img