2011-07-20 26 views
0

我想在struts中編寫小型登錄應用程序2.Session被創建成功。在welcome.jsp「註銷」選項給出。註銷控件將被重定向到Logout.jsp。 我的問題是註銷後會話變量被破壞,但頁面存儲在瀏覽器緩存中。如果單擊瀏覽器的後退按鈕,我可以看到welcome.jsp。 用於清除緩存「ClearCacheInterceptor」被使用。我不明白我在哪裏犯錯。 而不是每次都清除瀏覽器有沒有什麼可以克服這個問題?我的方法是否正確?請建議我。在struts2中登錄應用程序的問題

的Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Login</title> 
</head> 
<body> 
    <s:form action="login"> 
     <s:textfield name="myname"></s:textfield> 
     <s:submit value="submit"></s:submit> 
    </s:form> 
</body> 
</html> 

struts.xml中

<interceptors> 
      <interceptor name="clear-cache" class="ActionClasses.ClearCacheInterceptor" /> 
     </interceptors> 

     <action name="login" class="ActionClasses.LoginAction" > 
      <interceptor-ref name="clear-cache" /> 
      <result name="success">Welcome.jsp</result> 
      <result name="error">Login.jsp</result> 
     </action> 

     <action name="logout" class="ActionClasses.Logout"> 
      <interceptor-ref name="clear-cache" /> 
      <result name="success">Logout.jsp</result> 
     </action> 

LoginAction.java

package ActionClasses; 
import java.util.Map; 

import org.apache.struts2.interceptor.SessionAware; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; 
import com.opensymphony.xwork2.validator.annotations.ValidatorType; 
import java.util.Map; 

import javax.servlet.http.HttpServletResponse; 


import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.interceptor.Interceptor; 

public class LoginAction extends ActionSupport implements SessionAware 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private String myname; 
    private Map<String , Object> s; 

    public String execute()throws Exception 
    { 

      s=ActionContext.getContext().getSession(); 
      s.put("login", myname); 

     return "success"; 

    } 


    public void setMyname(String s) 
    { 

     myname=s; 

    } 

    public String getMyname() 
    { 
     return myname; 
    } 


    @Override 
    public void setSession(Map<String, Object> arg0) { 
     // TODO Auto-generated method stub 
     s=arg0; 
    } 
    } 

ClearcacheInterceptor.java

package ActionClasses; 





import javax.servlet.http.HttpServletResponse; 

import org.apache.struts2.StrutsStatics; 



import com.opensymphony.xwork2.ActionContext; 

import com.opensymphony.xwork2.ActionInvocation; 

import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 



public class ClearCacheInterceptor extends AbstractInterceptor{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Override 

    public String intercept(ActionInvocation invocation) throws Exception { 

     ActionContext context=(ActionContext)invocation.getInvocationContext(); 

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

     response.setHeader("Cache-Control", "no-cache"); 

     response.setHeader("Pragma", "no-cache"); 

     response.setDateHeader("Expires", 0); 

     String result=invocation.invoke(); 

     System.out.println("check result="+result); 

     return result; 

    } 

} 

Logout.java

package ActionClasses; 

import java.util.Map; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 


public class Logout extends ActionSupport { 



    public String execute(){ 

     Map<String,Object> s=ActionContext.getContext().getSession(); 



     s.remove("login"); 



     ActionContext.getContext().getSession().clear(); 



     return "success"; 
    } 



} 

Welcome.jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%@ page contentType="text/html; charset=UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<s:include value="CheckLogin.jsp"></s:include> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html> 
<head> 
</head> 
<body> 
<font color="white"></font> 
Welcome<s:property value="#session['login']"/> 
<s:a href="logout">Logout</s:a> 
</body> 
</html> 

Logout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 

log out successful !! 


</body> 
</html> 

CheckLogin.jsp

<%@ taglib prefix="s" uri="/struts-tags" %> 
<%@ page language="java" contentType="text/html" import="java.util.*"%> 
<html> 
    <head> 
    <title>Check validate!</title> 
    </head> 
    <body> 
    This is session validation page! 

    <s:if test="#session.login != 'Jagan'"> 
    <jsp:forward page="Login.jsp" /> 
    </s:if> 
    </body> 
</html> 
+0

這似乎有點毛病在struts.xml中的攔截器。 – Jagan

+0

註銷時刪除會話cookie可能是另一種解決方案,而不是每次都刪除緩存,但我不知道該怎麼做。 – Jagan

+0

嘗試在攔截器中添加'response.setHeader(「Cache-Control」,「no-store」);' – anu

回答

3

嗯,這是一個非常普遍的問題,這是機智的h尊重您的瀏覽器緩存問題,而不是struts2或任何其他框架。

我們遇到同樣的問題,因爲當您點擊瀏覽器的後退按鈕時,請求不會被髮送到服務器,而是從瀏覽器緩存中提供服務。您只會在嘗試做某些工作時注意到事情,它會出現錯誤,你不再登錄。

雖然你可以使用某些標題像no-cache等,但他們正在服從的瀏覽器是不確定的。 只能根據我的理解,解決此問題的方法是使用https(安全瀏覽)進行工作,而不是使用標題(no-cache,cache-expiry等),因爲當您在安全模式下瀏覽應用程序時,服務器和瀏覽器。

我希望這會盡量給你一個想法,只是爲了檢查重定向到HTTPS協議時,您的註銷,這將解決您的問題

+0

如何做到這一點(重定向ot https協議)?請給我提供任何有用的鏈接。 – Jagan

+0

在這裏使用struts2插件是同一個http://code.google.com/p/struts2-ssl-plugin/的URL –

相關問題