Tôi đang nhúng Jetty (phiên bản 7.4.5.v20110725) vào một ứng dụng java. Tôi đang phục vụ các trang JSP bằng ./webapps/jsp/ bằng cách sử dụng WebAppContext của Jetty, nhưng nếu tôi truy cập localhost: 8080/jsp/I, hãy lấy danh sách thư mục của Jetty cho toàn bộ nội dung của ./webapps/jsp/. Tôi đã thử thiết lập tham số dirAllowed thành false trên WebAppContext và nó không thay đổi hành vi liệt kê thư mục.Làm thế nào để tắt danh sách thư mục cho WebAppContext của Jetty?
Vô hiệu hóa danh sách thư mục trên ResourceHandler chỉ đơn giản là thực hiện được chuyển false đến setDirectoriesListed, hoạt động như mong đợi. Ai đó có thể cho tôi biết làm thế nào để làm điều này cho WebAppContext?
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
public class Test {
public static void main(String[] args) throws Exception {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setHost("127.0.0.1");
connector.setPort(8080);
server.addConnector(connector);
// Create a resource handler for static content.
ResourceHandler staticResourceHandler = new ResourceHandler();
staticResourceHandler.setResourceBase("./webapps/static/");
staticResourceHandler.setDirectoriesListed(false);
// Create context handler for static resource handler.
ContextHandler staticContextHandler = new ContextHandler();
staticContextHandler.setContextPath("/static");
staticContextHandler.setHandler(staticResourceHandler);
// Create WebAppContext for JSP files.
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/jsp");
webAppContext.setResourceBase("./webapps/jsp/");
// ??? THIS DOES NOT STOP DIR LISTING OF ./webapps/jsp/ ???
webAppContext.setInitParameter("dirAllowed", "false");
// Create a handler list to store our static and servlet context handlers.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { staticContextHandler, webAppContext });
// Add the handlers to the server and start jetty.
server.setHandler(handlers);
server.start();
server.join();
}
}
Hoạt động với Jetty 8.1.4.v20120524. Cảm ơn!!! – Tarlog
Và với 9.0.2.v20130417 – joscarsson
và với 9.2.x quá! – Nishant