如何在页面刷新后保持会话活动

how to keep session alive after page refresh

请问如何在用户刷新浏览器时保持主会话处于活动状态?

因为登录后,主页会话是活跃的。但是当我刷新浏览器时,它会让我回到登录页面。

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() {

        }

    }

请问如何在用户刷新浏览器时保持主会话处于活动状态?

因为登录后,主页会话是活跃的。但是当我刷新浏览器时,它会让我回到登录页面。

刷新页面时控制台是否有警告或错误?那么,对于 JSF 中的会话管理,我将执行以下操作:

1) 我将使用 ManagedBean @SessionScoped,一旦 s/he 登录,我会将用户信息保存到 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 而不从登录页面登录时验证会话:

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

那么您只需在渲染视图之前调用该方法即可:

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

4) 如果你想从任何 managedBean 中获取当前用户数据,你必须从 SessionMap 中获取它:

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

希望对您有所帮助 ;-)