I'm in the middle of migrating Servlet 2.5 based webapp to Servlet 3.0 and ran into this problem that I do not know how to squeeze these different filter-mappings into one @WebFilter annotation for the class.
Filters:
<!-- Day is 12 hours, so if we update at night the new content will be fetched first thing in the morning -->
<filter>
<filter-name>CacheForDay</filter-name>
<filter-class>x.web.client.filter.CacheHeaderFilter</filter-class>
<init-param>
<param-name>CacheTime</param-name>
<param-value>43200</param-value>
</init-param>
</filter>
<filter>
<filter-name>CacheForWeek</filter-name>
<filter-class>x.web.client.filter.CacheHeaderFilter</filter-class>
<init-param>
<param-name>CacheTime</param-name>
<param-value>604800</param-value>
</init-param>
</filter>
<filter>
<filter-name>CacheForMonth</filter-name>
<filter-class>x.web.client.filter.CacheHeaderFilter</filter-class>
<init-param>
<param-name>CacheTime</param-name>
<param-value>18144000</param-value>
</init-param>
</filter>
<filter>
<filter-name>noCache</filter-name>
<filter-class>x.web.client.filter.CacheHeaderFilter</filter-class>
<init-param>
<param-name>CacheTime</param-name>
<param-value>0</param-value>
</init-param>
</filter>
Filter-Mappings:
<filter-mapping>
<filter-name>CacheForDay</filter-name>
<url-pattern>*.png</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheForDay</filter-name>
<url-pattern>*.gif</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheForDay</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheForDay</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheForMonth</filter-name>
<url-pattern>*.ico</url-pattern>
</filter-mapping>
How can I replace them by @WebFilter?
It is impossible with a single class annotated with @WebFilter. You wil have to either:
@WebFilter. I think this wouldn't look nice.Example of the later:
public class BaseCacheFilter implements Filter
{
public void doFilter(...) {
...implementation here...
}
....
}
@WebFilter(
urlPatterns={"*.png","*.gif",...},
initParams=@WebInitParam(name="CacheTime",value="43200")
)
public class CacheForDay extends BaseCacheFilter
{ /* EMPTY */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With