@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {

            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse res = (HttpServletResponse) response;
            HttpSession ses = req.getSession(true);
            String reqURI = req.getRequestURI();
            //checks if the user the login, addUser page or if a session and a user are 
            //existing and not null, if it's null then redirect to index.html
            if (reqURI.indexOf("/jsf/login/login.xhtml") >= 0 || (ses != null && ses.getAttribute("username") != null)
                    || reqURI.indexOf("/public/") >= 0 || reqURI.contains("javax.faces.resource")) {
                chain.doFilter(request, response);
            } else {
                res.sendRedirect(req.getContextPath() + "/jsf/public/index.xhtml");
            }
        } catch (Throwable t) {
            System.out.println(t.getMessage());
        }
    }