-
Available since OmniFaces 1.0
The HttpFilter
is abstract filter specifically for HTTP requests. It provides a convenient abstract doFilter(HttpServletRequest, HttpServletResponse, HttpSession, FilterChain)
method directly providing the HTTP servlet request, response and session, so that there's no need to cast them everytime in the doFilter(ServletRequest, ServletResponse, FilterChain)
implementation. Also, default implementations of init(FilterConfig)
and destroy()
are provided, so that there's no need to implement them every time even when not really needed.
It's a bit the idea of using the convenient HttpServlet
abstract servlet class instead of the barebones Servlet
interface.
Usage
To use it, just let your custom filter extend from HttpFilter
instead of implement Filter
. For example:
@WebFilter("/app/*")
public class LoginFilter extends HttpFilter {
@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain)
throws ServletException, IOException
{
if (session != null && session.getAttribute("user") != null) {
chain.doFilter(request, response);
}
else {
Servlets.facesRedirect(request, response, "login.xhtml");
}
}
}