I'm creating a servlet filter which should only handle requests to specific paths in my application.
I can add a filter to my ServletContextHandler easily, for example like this:
FilterHolder holder = new FilterHolder(new MyFilter());
holder.setInitParameter("param", "a");
contextHandler.addFilter(holder, "/x", EnumSet.allOf(DispatcherType.class));
But how can I add more paths to this filter? I want it to handle requests to /y
as well.
I can't seem to find any easy and correct way to do this. Using a web.xml I would simply define multiple <url-pattern>
tags under the filter mapping.
Using Jetty version 9.3.0.v20150612.
Just add the filter (via its holder) multiple times.
FilterHolder holder = new FilterHolder(new MyFilter());
holder.setName("my-filter");
holder.setInitParameter("param", "a");
contextHandler.addFilter(holder, "/x", EnumSet.allOf(DispatcherType.class));
contextHandler.addFilter(holder, "*.y", EnumSet.allOf(DispatcherType.class));
contextHandler.addFilter(holder, "/z/*", EnumSet.allOf(DispatcherType.class));
Alternative technique (reusing holder generated from addFilter)
Enum<DispatcherType> dispatchers = EnumSet.allOf(DispatcherType.class);
FilterHolder holder = contextHandler.addFilter(MyFilter.class, "/x", dispatchers);
holder.setInitParameter("param", "a");
contextHandler.addFilter(holder, "*.y", dispatchers);
contextHandler.addFilter(holder, "/z/*", dispatchers);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.