Question

I am attempting to update a legacy Guice application, and I was wondering if there is any sort of preferred way of doing things when taking Servlet 3.0 annotations into consideration. For example, my application has a filter, FooFilter, which is defined in the Guice Module Factory method configureServlets(), as follows:

    Map<String, String> fooParams = new HashMap<String, String>();
    fooParams.put("someParam", "parameter information");                              
    filter("/foo.jsp","/foo/*").through(com.example.filter.FooFilter.class, fooParams);

Is the above binding still necessary, or will it interfere with the following using the @WebFilter Servlet 3.0 annotation:

    @Singleton
    @WebFilter(
        filterName="FooFilter",
        urlPatterns={"/foo.jsp", "/foo/*"},
        initParams = {
                    @WebInitParam(name="foo", value="Hello "),
                    @WebInitParam(name="bar", value=" World!")
                 })
    public class FooFilter implements Filter {
    etc....

Which method is now preferred? Will they mess with each other?

Was it helpful?

Solution

I just made quick draft how could a Servlet 3.0 support looks like. There could be a more elegant way to just call filter(Filter Class with WebFilter annotation) in configureServlet method, but that requires updated right to guice-servlet module, which is quite hard to distribute.

Well, what I did is a project at Github: https://github.com/xbaran/guice-servlet3

all you need to do is download and build. It is created on top of Guice 3.0 and works like this:

new Servlet3Module() {
      @Override
      protected void configureServlets3() {
        scanFilters(FooFilter.class.getPackage());
      }
};

The Servlet3Module extends ServletModule and contains a scanFilters method with package argument. This method will scan provided package from your classpath and try to register all classes with annotation WebFilter via filter() method.

This scan idea is based on Sitebricks (guice web framework created by Dhanji R. Prasanna) configuration system.

Honestly, I just make a draft, never try if it works. But hopefully it will. If you have any problem or question, just let me know.

PS: The support for servlets, listeners and so on could be added to, if you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top