You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "John D. Ament" <jo...@apache.org> on 2016/08/28 01:33:00 UTC
How to properly map a filter in tomcat embedded
Hi,
I have this pretty straight forward case of starting an instance of tomcat
and bringing in a filter.
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
File base = new File(".");
Context ctx = tomcat.addContext("", base.getAbsolutePath());
String filterName = "Default";
FilterDef filterDef = new FilterDef();
filterDef.setFilterName(filterName);
filterDef.setFilterClass(DefaultFilter.class.getName());
ctx.addFilterDef(filterDef);
FilterMap mapping = new FilterMap();
mapping.setFilterName(filterName);
mapping.addURLPattern("/*");
ctx.addFilterMap(mapping);
tomcat.start();
tomcat.getServer().await();
Thread.sleep(60000);
My filter's pretty boring, but here it is
public class DefaultFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse, FilterChain filterChain) throws IOException,
ServletException {
servletResponse.getWriter().println("Hello, world!");
}
@Override
public void destroy() {
}
}
When starting this up (Just running in a plain JUnit test), the container
is running however making requests to localhost:8080/ just result in
404's. I'd expect my filter to handle those requests.
When using a servlet, its perfectly fine.
I tried it on both 8.5.4 and 8.0.30.
Any ideas?
John
Re: How to properly map a filter in tomcat embedded
Posted by "John D. Ament" <jo...@apache.org>.
On Sun, Aug 28, 2016 at 5:07 AM Mark Thomas <ma...@homeinbox.net> wrote:
> On 28 August 2016 02:33:00 BST, "John D. Ament" <jo...@apache.org>
> wrote:
> >Hi,
> >
> >I have this pretty straight forward case of starting an instance of
> >tomcat
> >and bringing in a filter.
>
> <snip />
>
> >When starting this up (Just running in a plain JUnit test), the
> >container
> >is running however making requests to localhost:8080/ just result in
> >404's. I'd expect my filter to handle those requests.
> >
> >When using a servlet, its perfectly fine.
> >
> >I tried it on both 8.5.4 and 8.0.30.
> >
> >Any ideas?
>
> The Mapper (the component that Maps requests to the correct Host + Context
> + Servlet) expects to map a request to a Servlet and returns a 404 if none
> is found even if there is a filter that will handle the request.
>
> Normally you'd never see this behaviour because of the Default Servlet.
>
Ah ha, perfect tip! I can simply programmatically register default servlet
if none found. And that works!
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
Re: How to properly map a filter in tomcat embedded
Posted by Mark Thomas <ma...@homeinbox.net>.
On 28 August 2016 02:33:00 BST, "John D. Ament" <jo...@apache.org> wrote:
>Hi,
>
>I have this pretty straight forward case of starting an instance of
>tomcat
>and bringing in a filter.
<snip />
>When starting this up (Just running in a plain JUnit test), the
>container
>is running however making requests to localhost:8080/ just result in
>404's. I'd expect my filter to handle those requests.
>
>When using a servlet, its perfectly fine.
>
>I tried it on both 8.5.4 and 8.0.30.
>
>Any ideas?
The Mapper (the component that Maps requests to the correct Host + Context + Servlet) expects to map a request to a Servlet and returns a 404 if none is found even if there is a filter that will handle the request.
Normally you'd never see this behaviour because of the Default Servlet.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org