2017-03-06 211 views
0

請問當用戶刷新瀏覽器時,我應該如何保持主頁會話的活動狀態?如何在刷新頁面後保持會話活動狀態

因爲登錄後,主頁會話是活着的。但是,當我刷新瀏覽器時,它將我帶回登錄頁面。

import java.io.IOException; 
    import javax.servlet.Filter; 
    import javax.servlet.FilterChain; 
    import javax.servlet.FilterConfig; 
    import javax.servlet.ServletException; 
    import javax.servlet.ServletRequest; 
    import javax.servlet.ServletResponse; 
    import javax.servlet.annotation.WebFilter; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 

    /** 
    * 
    * @author Maxwell 
    */ 
    @WebFilter(filterName = "sessionFilter", urlPatterns = {"/*"}) 
    public class sessionFilter implements Filter { 

     @Override 
     public void init(FilterConfig filterConfig) throws ServletException { 

     } 

     @Override 
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
      HttpServletRequest req1 =(HttpServletRequest)request; 
      HttpServletResponse res1 =(HttpServletResponse)response; 

      String session = (String)req1.getSession().getAttribute("pnumber"); 
      String currentPath = req1.getRequestURL().toString(); 

      if(session != null) 
      { 
       if(currentPath.contains("login.xhtml")) 
       { 
        res1.sendRedirect(req1.getContextPath()+"/home.xhtml"); 
        System.out.println("it is nt empty"); 
       } 
       else 
       { 
        chain.doFilter(request, response); 
       } 
       //System.out.println("it is nt empty"); 
      } 
      else 
      { 
       if(currentPath.contains("home")) 
       { 
        res1.sendRedirect(req1.getContextPath()+"/login.xhtml"); 
        System.out.println("somefin is wrong"); 
       } 
       else 
       { 
        chain.doFilter(request, response); 
       } 
       //System.out.println("somefin is wrong"); 
      } 

     } 

     @Override 
     public void destroy() { 

     } 

    } 

當用戶刷新瀏覽器時,我該如何保持家庭會話?

因爲登錄後,主頁會話是活着的。但是,當我刷新瀏覽器時,它將我帶回登錄頁面。

+0

有沒有** jsf在您發佈的代碼中,只是一個servlet過濾器 – Kukeltje

回答

1

刷新頁面時,控制檯中是否有任何警告或錯誤?那麼,對於JSF中的會話管理,我會執行以下操作:

1)我會使用ManagedBean @SessionScoped,在用戶信息登錄後將其保存到SessionMap中:

public void login(){ 
    //your code 
    if(validations){ 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    fc.getExternalContext().getSessionMap().put("loggedUser", User); 
    //User is the entity which contains the current user info. 
    //You can use any data to keep in the SessionMap ;-) 
    } 
} 

2)註銷用戶點擊,你有失效會話:

FacesContext fc = FacesContext.getCurrentInstance(); 
fc.getExternalContext().invalidateSession(); 

3)現在,如果你想驗證會話當用戶鍵入直接的URL,而不從登錄頁面登錄:

<f:event type="preRenderView" listener="#{loginBean.validateSession()}" /> 

public void validateSession(){ 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    User currentUser = (User) fc.getExternalContext().getSessionMap().get("loggedUser"); 
    if(currentUser==null){ 
     //shows a message 'Session has caducated' 
     fc.getExternalContext().redirect("./login.xhtml"); 
    } 
} 

然後你只需渲染視圖之前調用該方法

4)如果您想獲得任何managedBean當前用戶的數據,你必須從SessionMap得到它:

FacesContext fc = FacesContext.getCurrentInstance(); 
User user = (User) fc.getExternalContext().getSessionMap().get("loggedUser"); 

希望這可以幫助;-)

+0

謝謝;-),我已經完成了所有。但是,當用戶登錄並在用戶頁面上時,如果他決定刷新瀏覽器,他會自動重新導向回登錄頁面。即使在刷新瀏覽器之後,我仍然希望用戶維護用戶頁面 – Maxwell

相關問題