@Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);

        if (session == null || session.getAttribute("user") == null) {
            String requesturl = request.getRequestURI();
            if (requesturl.endsWith("index.jsp")) {
                chain.doFilter(req, res);
            } else {
                response.sendRedirect(request.getContextPath() + "/index.jsp"); // No logged-in user found, so redirect to login page.
            }
        } else {
            UserDAO.User currentUser = (UserDAO.User) session.getAttribute("user");
            session.setAttribute("usertype", currentUser.usertype);

            chain.doFilter(req, res); // Logged-in user found, so just continue request.
        }
    }