如果会话不存在,如何将jsf页面重定向到主页

时间:2014-04-26 07:49:26

标签: java jsf

创建一个JSF应用程序,我在主页面中嵌入了一个applet。我的问题是,我无法弄清楚如何在将applet加载到主页面之前检查会话是否存在,如果会话不存在,我想将用户重定向到主页

1 个答案:

答案 0 :(得分:1)

我建议使用过滤器:

public class LoggedFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpSession session = httpServletRequest.getSession(false);
        if (session == null) {
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            httpServletResponse.sendRedirect(httpServletResponse.encodeRedirectURL(httpServletRequest.getContextPath() + "/"));
        } else {
            chain.doFilter(request, response);
        }
    }

当然,将此过滤器映射到web.xml中的所有JSF页面:

<filter>
  <filter-name>LoggedFilter</filter-name>
  <filter-class>LoggedFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>LoggedFilter</filter-name>
  <url-pattern>*.xhtml</url-pattern>
</filter-mapping>

(我假设JSF页面有后缀xhtml)