1

Am creating a JSF application where I have embedded a applet in the main page. my problem is that i cant figure out how to check if a session exist before loading the applet into the main page and if the session does not exist I want to redirect the user to home page

  • A similar question might help http://stackoverflow.com/questions/8144195/check-if-session-exists-jsf – AurA Apr 26 '14 at 08:36

1 Answers1

1

I would recommend to use filter:

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

And of course map this filter to all JSF pages in web.xml:

<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>

(I assume that JSF pages have suffix xhtml)

Leos Literak
  • 8,805
  • 19
  • 81
  • 156