2013-01-18 32 views
20

Tôi đang nhúng cầu nối vào ứng dụng của mình và cố gắng tìm cách thêm bộ lọc servlet (để xử lý cookie). Wiki và dont của javadoc làm cho nó rất rõ ràng, tôi đang thiếu gì:Cách thêm Bộ lọc servlet với cầu nối nhúng

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()), "/"); 

Thông báo thông tin duy nhất tôi đã tìm thấy trên đây là một forum post suggesting the documentation về vấn đề này cần phải được cải thiện.

+0

Có một lý do bạn không thể định nghĩa nó trong tập tin web.xml. Tôi nhận ra điều này được nhúng nhưng miễn là bạn có tập tin trong classpath theo WEB-INF/web.xml bạn nên được tốt. –

+6

Tôi đã không sử dụng web.xml trong một thời gian dài, thông thường tôi chỉ sử dụng chú thích đặc tả servlet 3.0. Tôi chỉ không thích mucking xung quanh với các tập tin XML. – Jacob

Trả lời

23

Cập nhật: Đối với Jetty phiên bản 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); 
    } 

câu trả lời gốc ===

Nếu bạn không muốn sử dụng web.xml sau đó sử dụng này:

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(); 

Nếu bạn muốn sử dụng web.xml, thay vì các phương thức addFilter(), chỉ cần đảm bảo rằng bạn có một WEB-INF/web.xml trong đường dẫn gốc webapp của bạn với xml sau đây:

<?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

Cảm ơn. 1 có ý nghĩa gì trong 'webapp.addFilter (MyFilter.class,"/test ", 1)'. Trong mã của tôi tôi chỉ bỏ qua nó và thông qua trong NULL, mà dường như cũng làm việc. – Jacob

+0

Tham số có giá trị là 1 là số lượng phiên bản khởi chạy khi khởi động. –

+0

như tôi đã hiểu cầu cảng 9.1.3, tham số cuối cùng là một EnumSet : Được khai báo trong ServletContextHandler Bộ lọc công cộngHọc addFilter (Lớp filterClass, String pathSpec, EnumSet dispatches) –

23

Tôi gặp vấn đề tương tự, nhưng tôi nghĩ câu trả lời của Καrτhικ quá phức tạp. Tôi đã tìm thấy cách này dễ dàng:

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(); 

Phiên bản cầu tàu của tôi là 8.1.14.v20131031.

+3

vâng, câu trả lời khác có rất nhiều thứ không liên quan cho câu hỏi thực tế. – Renato

2

Phương thức ServletContextHandler.addFilter(...) chỉ là các trình bao bọc tiện lợi xung quanh các phương pháp ServletHandler.addFilter(...). Miễn là bạn chỉ cần một <url-pattern>, chúng khá thuận tiện. Tuy nhiên, nếu bạn cần nhiều hơn một mẫu hoặc chọn sử dụng <servlet-name> thay vào đó, bạn sẽ cần một cái gì đó như thế này:

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); 
Các vấn đề liên quan