Tagged: servlet-filters

Servlets: filtering

You can filter all servlet requests, including REST ones with Jersey, with a filter:

 public class HelloFilter implements Filter {
  public void destroy() {
  public void doFilter(ServletRequest req, ServletResponse resp, 
           FilterChain chain) throws IOException, ServletException {
    chain.doFilter(req, resp);
  public void init(FilterConfig arg0) throws ServletException {

In the doFilter() method you call the rest of the filters on the chain to continue. Alternatively, you can just bail out there with something like:

  HttpServletResponse r = (HttpServletResponse) resp;

The filter allows you do look at the request and response, do various things with the servletcontext or httpservletrequest the latterly called doGet or REST method will see such changes.

You can register this in your web.xml like so:

servlet servlet-filters

Page 1 of 1