I'm using embedded Jetty to create a static web site. Does ServletContextHandler.setResourceBase("...") have a same effect as ResourceHandler.setResourceBase("...")?
Example:
// ServletContextHandler case
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setResourceBase("/tmp/...");
// ResourceHandler case
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase("/tmp/...");
I have tried test both of them. ResourceHandler works exactly what I want. But otherwise doesn't. What's the difference between them?
(Sorry for my poor English :P)
Update
After changed, below is whole code. Context("/") serves static files, wsContext("/ws") serves web socket endpoints. Of course Context("/") is able to serve web socket endpoints as well.
server = new Server();
server.setStopAtShutdown(true);
ServerConnector connector = new ServerConnector(server);
connector.setPort(8000);
server.addConnector(connector);
// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("webapp.path"));
ServletContextHandler wsContext = new ServletContextHandler();
wsContext.setContextPath("/ws");
ContextHandlerCollection contexts=new ContextHandlerCollection();
contexts.setHandlers(new Handler[]{context, wsContext});
server.setHandler(contexts);
context.addServlet(DefaultServlet.class, "/");
// Initialize javax.websocket layer
ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext(wsContext);
// Add WebSocket endpoint to javax.websocket layer
// code omitted...
server.start();
logger.info("WebServer started.");
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…