Jetty如何将Servlet添加到多个ServletContextHandler或将ContainerRequestFilter应用于ContextHandlerCollection [英] Jetty How to add a servlet to multiple ServletContextHandler or apply ContainerRequestFilter to ContextHandlerCollection

查看:157
本文介绍了Jetty如何将Servlet添加到多个ServletContextHandler或将ContainerRequestFilter应用于ContextHandlerCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ContextHandlerCollectionContainerRequestFilter中有多个ServletContextHandler. 我需要将此ContainerRequestFilter添加到所有ServletContextHandler 我发现添加ContainerRequestFilter的唯一方法是通过ResourceConfig.所以我这样做了:

I have multiple ServletContextHandler in a ContextHandlerCollection and a ContainerRequestFilter. I need this ContainerRequestFilter to be added to all ServletContextHandler Only way I could find of adding the ContainerRequestFilter was through ResourceConfig. So I did this:

ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(MyContainerRequestFilter.class);
ServletHolder s = new ServletHolder(new ServletContainer(resourceConfig));
for (Handler context : contextHandlers) {
    ((ServletContextHandler)context).addServlet(s, "/*");
} 

其结果是:

java.lang.IllegalStateException:多个servlet映射到路径:/*: org.eclipse.jetty.proxy.ProxyServlet $ Transparent-56c0a61e

java.lang.IllegalStateException: Multiple servlets map to path: /*: org.eclipse.jetty.proxy.ProxyServlet$Transparent-56c0a61e

正确的方法是什么?

我也研究了处理程序并尝试遵循,但是它覆盖了ContextHandlerCollection中包含的所有其他servlet,即,当我调用/api(存在于ContextHandlerCollectionServletContextHandler之一中)时,由于404 context.setContextPath("/");,但是无论如何都需要在基本路径上应用此请求过滤器.

I also looked into handlers and tried following but it overrides all the other servlets contained in ContextHandlerCollection i.e., when I call /api (exists in one of the ServletContextHandler in ContextHandlerCollection), I get 404 because of context.setContextPath("/"); below, but then this request filter needs to be applied on base path anyway.

HandlerWrapper wrapper = new HandlerWrapper();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(RequestInterceptor.class);
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
wrapper.setHandler(context)
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handler[]{wrapper,contexts});

我还尝试将过滤器添加到上述集合中:

I also tried adding filter to above collection:

HandlerWrapper wrapper = new HandlerWrapper();
FilterHolder filter = new FilterHolder(MyContainerRequestFilter.class);  // had to implment filter interface
wrapper.addFilterWithMapping(filter, "/*", EnumSet.allOf(DispatcherType.class)) ;
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handler[]{contexts,wrapper});

在这种情况下,请求确实到达了过滤器,但出现以下异常:

In this case request does come to the filter but I get following exception:

无法发送响应错误500:java.lang.IllegalStateException: 已提交404前为空

Could not send response error 500: java.lang.IllegalStateException: Committed Committed before 404 null

推荐答案

我无法使用ContainerRequestFilter做到这一点,但是我不得不使用javax.servlet.Filter

I could not do it with ContainerRequestFilter but I had to use javax.servlet.Filter

在我的情况下(多个ServletContextHandler)添加javax.servlet.Filter的正确方法是:

And the correct way to add a javax.servlet.Filter in my case (Multiple ServletContextHandler) is:

Handler[] contextHandlers = contexts.getHandlers();

for (Handler context : contextHandlers) {
        ((ServletContextHandler)context).addFilter(RequestInterceptor.class, "/*", 
EnumSet.allOf(DispatcherType.class));
}

这篇关于Jetty如何将Servlet添加到多个ServletContextHandler或将ContainerRequestFilter应用于ContextHandlerCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆