码头,未调用doFilter方法

时间:2018-07-23 12:55:55

标签: java servlet-filters embedded-jetty

我已经设置了一个带有过滤器的嵌入式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”进行了尝试。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

请勿像这样混合使用HandlersServletContextHandler's

这不是ServletContextHandler.setHandler(Handler)的目的。

MainHandlerHttpServlet扩展到ServletContextHandler.addServlet()

这是因为ServletContextHandler.setHandler()用于管理ServletContext之外的事物以及特定的请求链(例如会话管理,安全性管理,gzip编码管理,请求日志记录管理等),而不是用于实际处理请求并在该ServletContext中产生响应。

答案 1 :(得分:0)

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