How to map a filter to a servlet using guice-servlet?

819 views Asked by At

With Guice-servlet we can easily do servlet mapping like this: filter(*.jsp).through(MyFilter.class)

However could anyone tell me how can i map a filter to a servlet?

<filter-mapping>
  <filter-name>Reporter</filter-name>
  <servlet-name>SomeServletName</servlet-name>
</filter-mapping>

Thanks

2

There are 2 answers

0
Martin Probst On

That is not possible using Guice, as far as I can tell. You can serve URLs or filter URLs, but you cannot bind a filter to a specific servlet.

However if your servlet requires a particular processing of the request, maybe it might even be clearer to just do that call in code, instead of configuration? Filters are good if you need request processing that's used across many (all) servlets; if you need something specific to a particular servlet, you might be better off just calling the logic in question from you code.

0
opncow On

I know it does not really help you, but according to this statement:

You need to specify a filter. We deliberately chose not to use the name binding route for type safety and clarity. Otoh, we support regex matching so that should be fairly easy.

taken from here by Dhanji (I think one of the creators of the servlet extension):

https://groups.google.com/forum/?fromgroups#!topic/google-guice/njYOI32K1us

It might not be possible out of the box. Although this post is a few years old. So maybe something changed already.