How to add servlet Filter with embedded jetty

后端 未结 3 1968
清歌不尽
清歌不尽 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: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.

提交回复
热议问题