Home > database >  How to register an HttpFilter?
How to register an HttpFilter?

Time:08-03

I've created an HttpFilter in my project:

ExampleFilter.java

package contoso.grobber;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("")
public class ExampleFilter extends HttpFilter {

    @Override
    public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) 
    {
        beginRequest(req); //before request processing
        chain.doFilter(req, res);//calls other filters and processes request
        endRequest(req, res); //after request processing you can use res here
    }

    private void beginRequest(HttpServletRequest request) {
       //...
    }

    private void endRequest(HttpServletRequest request, HttpServletResponse response) {
       //...
    }
}

But the filter just isn't being called.

Supposedly using the @WebFilter annotation is enough to make it work:

What's the alternative?

I've heard tell of modifying web.xml to register the filter; but that is for a Filters - and not an HttpFilters.

Day 12 of trying to get this to work...:

CodePudding user response:

Converting the relevant parts of my comments to an answer here:


Short Answer

You need to provide one or more values in the string of the @WebFilter annotation, to represent the URL pattern(s) to which the filter applies.

So, for example, the following refers to all URLs under the root of your app:

@WebFilter("/*")

And the following refers to one specific URL:

@WebFilter("/demo_one")

Longer Answer

The @WebFilter annotation is part of the Jakarta Servlet specification. For example, here is the Web Filter section for version 5.0 of the spec.

The empty string used in @WebFilter("") is a special pattern, documented here:

The empty string ("") is a special URL pattern that exactly maps to the application’s context root, i.e., requests of the form http://host:port/<context-root>/. In this case the path info is "/" and the servlet path and context path is the empty string ("").

Other rules for URL patterns are also covered here, including wildcards (*).


The JavaDoc for @WebFilter lists all of the attributes you can use in the annotation.

(As you note, the JavaDoc does not make it especially clear which of these attributes is mandatory, or explain how they work in any detail.)

One of these attributes is value - an array of strings:

The URL patterns to which the filter applies. The default value is an empty array.

So, instead of @WebFilter("/demo_one") you can use:

@WebFilter(value = "/demo_one")

Or, for an array of URL patterns:

@WebFilter(value = {"/demo_one", "/demo_two"})

If you don't use the value = specifier (i.e. if you only use a string and nothing else), then the annotation assumes the provided string is the one provided mandatory value (one of urlPatterns, servletNames, or value).

You can see there is a fairly long list of other attributes you can specify in the annotation - they are all the equivalents of the values that you can assign in the XML of a web.xml file, if you use that instead of annotations.

  • Related