關於Session過期,我在web.xml中填寫了filter項目,但由於自己寫的filter類不能獲取到FacesContext或Session對象,不知道該怎樣在代碼中判斷session是否過期,也不知道什麽代碼能實現葉面的跳轉。望大家指點。謝謝!

解决方案 »

  1.   

    给你看个我写的简单例子,就是判断是否有uer,就是看用户是否登录的,没有登录的回调到登录页面
      

  2.   

    public class HtmlFilter implements Filter  { protected FilterConfig filterConfig; 
      private String loginPage; 
      protected TUser _user;   public void init(FilterConfig config) throws ServletException { 
        this.filterConfig = config; 
        loginPage = config.getInitParameter("loginPage"); 
        if (loginPage == null) { 
          throw new ServletException("loginPage init param missing"); 
        } 
      } 
      public void doFilter(final ServletRequest req, final ServletResponse 
                           res, FilterChain chain) throws IOException, 
          ServletException { 
        HttpServletRequest hreq = (HttpServletRequest) req; 
        HttpServletResponse hres = (HttpServletResponse) res; 
        String url = hreq.getRequestURI()+"?"+hreq.getQueryString();
    UserInfoDAO userInfoDAO = new UserInfoDAO();

        _user = (TUser)hreq.getSession().getAttribute("_USER");
    //     String isLog = (String) hreq.getSession().getAttribute("validsession");
    //     if ( (isLog != null) && ( (isLog.equals("true")) || (isLog == "true"))) { //检查是否登录 
        if (_user!=null){
          TUserinfo userInfo=userInfoDAO.findUserInfo(_user,"1");
          chain.doFilter(req, res); 
          return; 
        } 
        else 
          hres.sendRedirect(loginPage);  
      } 
      public void destroy() { 
        this.filterConfig = null; 
      }    public void setFilterConfig(final FilterConfig filterConfig) { 
        this.filterConfig = filterConfig; 
      }   }
      

  3.   

    謝謝SDMRauquin(冷月无心) 的回答,還有一個問題就是關於你這個Filter的配置XML是怎麽寫的。如果這個Filter的範圍是全局的話,即使session過期sendRedirect到login頁面,但進入login頁面之前仍然要走一遍這個filter,結果就是又被sendRedirect到login頁面了!filter就又會被執行,這樣就是無限循環。不知道您是否看明白我所說的,呵呵。請再次幫我解釋一下,十分感謝!!!!