我嵌入码头到我的应用程序,并试图找出如何添加Servlet过滤器(用于cookie处理)。 维基和javadoc中的不要让它很清楚,我在想什么:
Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
FilterHolder f = new FilterHolder(new AuthorisationFilter());
context.addFilter(... f ...); // ?????
context.addServlet(new ServletHolder(new TestServlet()), "/");
我已经在这个发现的唯一信息是一个论坛的帖子暗示的文档需要在此加以改进。
更新:对于码头的版本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);
}
原来的答案===
如果你不想使用web.xml文件,然后使用此:
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();
如果你想使用的web.xml,而不是addFilter()方法的方法,只要确保你有一个WEB-INF / web.xml文件中有以下的XML你的web应用的根路径:
<?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>
我得到了同样的问题,但我认为Καrτhικ的回答是太复杂了。 我发现这个简单的方法:
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();
我的码头的版本是8.1.14.v20131031
。
该ServletContextHandler.addFilter(...)
方法是围绕只是方便的包装ServletHandler.addFilter(...)
方法。 假如你只需要一个<url-pattern>
,他们都相当方便。 不过,如果你需要一个以上的图案或选择使用<servlet-name>
相反,你将需要更多的东西是这样的:
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 );