Servlet 3.0 annotations in conjuction with Guice
Asked Answered
S

1

6

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?

Shoeshine answered 9/11, 2013 at 4:5 Comment(0)
S
0

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.

Sabella answered 19/11, 2013 at 13:46 Comment(5)
Milan, have you used this in production or testing? Are there any hiccups after the scanning is complete?Shoeshine
I just checked out the code, and it answers my question indirectly in that the servlets and filters must still be registered through Guice. Thanks.Shoeshine
Hi, Guice do not support Servlet 3.0 annotations directly yet. So, this is kind of workaround, but it does what you need. As you said, I just using Guice API to register filters so, there should be no problem to use it in production.Sabella
Milan, don't you think this way of doing can interfer with a true Servlet 3.0 container. This solution will work with servlet container < 3.0 . I mean servlet 3.0 container will also try to register the servlet with their own scan mechanism. I think in that case we have to plug to API/SPI of JEE . What do you think ?Bb
taharqa question makes much sense.Thoracotomy

© 2022 - 2024 — McMap. All rights reserved.