2012-05-16 91 views
0

四處錯誤:java.lang.IllegalStateException:getAttributeNames:會話已經失效

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"; 
     } 

LoginInterceptor

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(); 
     } 




    } 

的login.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時所述。

回答

1

攔截器可以在調用Action之前和之後執行代碼。你的什麼時候被調用?我會想象發生的事情是這樣的:

  • 在LogOutUserAction.java你無效會話,然後執行
  • LoginInterceptor.intercept。這會嘗試從您失效的會話對象中獲取屬性,因此會導致IllegalStateException。

你爲什麼使會話無效?您的LoginInterceptor正在使用USER_HANDLE屬性來指示用戶是否已登錄。我將在LogOutUserAction中刪除此屬性,但不會使會話無效。

相關問題