Jetty,doFilter 方法没有被调用

Jetty, doFilter method not getting called

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

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 是处理请求的class。 调用过滤器的 init() 方法,但不调用 doFilter() 方法,因此不应用我的过滤器。我用“/application/something”试过了。

有人可以帮我吗?

不要那样混合 HandlersServletContextHandler's

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

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

您的 MainHandler 阻止将请求正确发送到 ServletContextHandler

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

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>");
        }
    }
}