【问题标题】:Jetty, doFilter method not getting called码头,doFilter 方法没有被调用
【发布时间】:2018-07-23 12:55:55
【问题描述】:

我已经设置了一个带有过滤器的嵌入式 Jetty 服务器,以便为每个请求添加一些标头。

MainHandler mainHandler = new MainHandler();
        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        servletContextHandler.setContextPath("/application");
        servletContextHandler.setHandler(mainHandler);
        servletContextHandler.setAllowNullPathInfo(true);
        servletContextHandler.addFilter(MyFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.INCLUDE, DispatcherType.FORWARD));

Mainhandler 是处理请求的类。 过滤器的 init() 方法被调用,而不是 doFilter() 方法,因此我的过滤器没有被应用。我用“/application/something”试过了。

有人可以帮帮我吗?

【问题讨论】:

    标签: java servlet-filters embedded-jetty


    【解决方案1】:

    不要像这样混合HandlersServletContextHandler's

    这不是ServletContextHandler.setHandler(Handler) 的用途。

    将您的MainHandler 更改为从HttpServlet 扩展并将其添加到ServletContextHandler.addServlet()

    您的MainHandler 阻止将请求正确分派到ServletContextHandler

    这是因为ServletContextHandler.setHandler()是用于管理ServletContext之外的东西和具体的请求链(如会话管理、安全管理、gzip编码管理、请求日志管理等),不用于实际处理一个请求并在该ServletContext 中产生一个响应。 ServletContextHandler.setHandler() 用于管理交换中的状态和范围(请求、响应、输入、输出、会话、安全等),所有有效的实现都是 ScopedHandler,即 ServletContextContextServletContextHandler 知道。

    【讨论】:

    • 不太清楚为什么这是公认的答案。虽然它可能评估为真,但它不能回答问题。
    • @Midiman 答案足以回答问题(MainHandler 是不良行为的原因),原始答案提供了修复。更新后的答案提供了更多细节(除非被问到,否则我不认为人们想要很长的细节)。
    【解决方案2】:

    this官方文档的例子:

    package org.eclipse.jetty.embedded;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.servlet.ServletHandler;
    
    public class MinimalServlets
    {
        public static void main( String[] args ) throws Exception
        {
            // Create a basic jetty server object that will listen on port 8080.
            // Note that if you set this to port 0 then a randomly available port
            // will be assigned that you can either look in the logs for the port,
            // or programmatically obtain it for use in test cases.
            Server server = new Server(8080);
    
            // The ServletHandler is a dead simple way to create a context handler
            // that is backed by an instance of a Servlet.
            // This handler then needs to be registered with the Server object.
            ServletHandler handler = new ServletHandler();
            server.setHandler(handler);
    
            // Passing in the class for the Servlet allows jetty to instantiate an
            // instance of that Servlet and mount it on a given context path.
    
            // IMPORTANT:
            // This is a raw Servlet, not a Servlet that has been configured
            // through a web.xml @WebServlet annotation, or anything similar.
            handler.addServletWithMapping(HelloServlet.class, "/*");
    
            // Start things up!
            server.start();
    
            // The use of server.join() the will make the current thread join and
            // wait until the server is done executing.
            // See
            // http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
            server.join();
        }
    
        @SuppressWarnings("serial")
        public static class HelloServlet extends HttpServlet
        {
            @Override
            protected void doGet( HttpServletRequest request,
                                  HttpServletResponse response ) throws ServletException,
                                                                IOException
            {
                response.setContentType("text/html");
                response.setStatus(HttpServletResponse.SC_OK);
                response.getWriter().println("<h1>Hello from HelloServlet</h1>");
            }
        }
    }
    

    【讨论】:

    • 那不是ServletContextHandler,那是ServletHandler
    • 注意:ServletHandlerServletContextHandler 的内部类,没有ServletContext,因此不能有任何需要它的东西(如Filters、会话、安全性或任何东西否则真的)。上面的demo只对1个Servlet有效,就是这样。
    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 2012-03-13
    • 2014-06-02
    • 2011-08-28
    • 2017-02-24
    相关资源
    最近更新 更多