How to add servlet Filter with embedded jetty

后端 未结 3 1947
清歌不尽
清歌不尽 2020-12-14 16:26

I am embedding jetty into my app, and trying to work out how to add servlet filters (for cookie handling). The wiki and the javadoc\'s dont make it very clear, what am I mis

相关标签:
3条回答
  • 2020-12-14 17:10

    Update: For Jetty version 9.2.2:

        Server server = new Server();
    
        // Note: if you don't want control over type of connector, etc. you can simply 
        // call new Server(<port>);
        ServerConnector connector = new ServerConnector(server);
        connector.setHost("0.0.0.0");
        connector.setPort(8085);
        // Setting the name allows you to serve different app contexts from different connectors.
        connector.setName("main");
        server.addConnector(connector);
    
        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        // For development within an IDE like Eclipse, you can directly point to the web.xml
        context.setWar("src/main/webapp");
        context.addFilter(MyFilter.class, "/", 1);
    
        HandlerCollection collection = new HandlerCollection();
        RequestLogHandler rlh = new RequestLogHandler();
        // Slf4j - who uses anything else?
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(false);
        rlh.setRequestLog(requestLog);
        collection.setHandlers(new Handler[] { context, rlh });
        server.setHandler(collection);
    
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            // Google guava way
            throw Throwables.propagate(e);
        }
    

    Original answer ===

    If you don't want to use web.xml then use this:

    SocketConnector socketConnector = new SocketConnector();
    socketConnector.setPort(7000); // Change to port you want
    Server server.setConnectors(new Connector[] { socketConnector });
    
    WebAppContext webapp = new WebAppContext();
    
    webapp.setContextPath("/"); // For root
    webapp.setWar("/"); // Appropriate file system path.
    
    // Now you can use the various webapp.addFilter() methods
    webapp.addFilter(MyFilter.class, "/test", 1); // Will serve request to /test.
    // There are 3 different addFilter() variants.
    
    // Bonus ... request logs.
    RequestLogHandler logHandler = new RequestLogHandler();
    NCSARequestLog requestLog = new NCSARequestLog("/tmp/jetty-yyyy_mm_dd.request.log");
    requestLog.setRetainDays(90);
    requestLog.setAppend(true);
    requestLog.setExtended(false);
    requestLog.setLogTimeZone("GMT");
    logHandler.setRequestLog(requestLog);
    
    logHandler.setHandler(webapp);
    
    HandlerList handlerList = new HandlerList();
    handlerList.addHandler(logHandler);
    
    server.setHandler(handlerList);
    
    server.start();
    

    If you do want to use web.xml, instead of the addFilter() methods, just make sure you have a WEB-INF/web.xml in your webapp root path with the following xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
       PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
        <filter>
            <filter-name>filterName</filter-name>
            <filter-class>com.x.y.z.FilterClass</filter-class>
        </filter>
        <filter-mapping>
            <url-pattern>/test</url-pattern>
            <filter-name>filterName</filter-name>
        </filter-mapping>
    </web-app>
    
    0 讨论(0)
  • 2020-12-14 17:15

    I got the same problem, but I think Καrτhικ's answer is too complex. I found this easy way:

    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
    context.addFilter(AppFilter.class, "/*", EnumSet.of(DispatcherType.INCLUDE,DispatcherType.REQUEST));
    
    server.setHandler(context);
    server.start();
    server.join();
    

    My jetty version is 8.1.14.v20131031.

    0 讨论(0)
  • 2020-12-14 17:18

    The ServletContextHandler.addFilter(...) methods are just convenience wrappers around the ServletHandler.addFilter(...) methods. Provided that you only need one <url-pattern> they are quite convenient. However, if you need more than one pattern or choose to use <servlet-name> instead, you will need something more like this:

    ServletContextHandler context = new ServletContextHandler(
            ServletContextHandler.SESSIONS);
    
    FilterMapping mapping = new FilterMapping();
    mapping.setFilterName( "Foobar Filter" );
    mapping.setPathSpecs( new String[] { "/foo/*", "/bar/*" } );
    mapping.setServletNames( new String[] { "foobar" } );
    mapping.setDispatcherTypes(
            EnumSet.of( DispatcherType.INCLUDE,DispatcherType.REQUEST ) ) );
    
    FilterHolder holder = new FilterHolder( FoobarFilter.class );
    holder.setName( "Foobar Filter" );
    
    context .getServletHandler().addFilter( holder, mapping );
    
    0 讨论(0)
提交回复
热议问题