What is the difference between ServletContextHandler.setResourceBase and ResourceHandler.setResourceBase when using the built-in Jetty container?

I use the built-in Jetty to create a static website. Does ServletContextHandler.setResourceBase ("...") have the 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 tried checking both of them. ResourceHandler works exactly the way I want. But otherwise not. What is the difference between the two?

(Sorry for my bad English: P)

Update

After the change, the whole code is presented below. Context ("/") serves for static files, wsContext ("/ ws") serves for endpoints of a web socket. Of course, context ("/") can also serve endpoints on web sockets.

  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."); 
+4
java jetty
Feb 09 '15 at 20:13
source share
1 answer

With this setting, resourceHandler will never be called, since the processing of the DefaultServlet (or Default404Servlet ) at the end of the ServletContextHandler chain will always respond, preventing the resourceHandler even executing.

If you have a ServletContextHandler , do not use resourceHandler use the DefaultServlet in this ServletContextHandler to configure and maintain your static files.

resourceHandler very simplified if you want more controls, use the DefaultServlet configured instead of ServletContextHandler .

Okay, with that out of the way ...

ServletContextHandler.setBaseResource(Resource) is the place for ServletContext itself to configure its contextual resourceBase .

(Note: the setResourceBase () parameter is a URL string that can point to the file:// directory or even to jar:file:// . Almost everything that is supported by Resource.newResource(String) )

  • ${resourceBase}/ is the search point for various methods in javax.servlet.ServletContext , for example:
    • String getRealPath(String path)
    • URL getResource(String path)
    • InputStream getResourceAsStream(String path)
    • Set<String> getResources(String path)
  • Requested resources that do not match any of your servlets or filters will be processed using DefaultServlet , which can serve static resources (such as *.html , *.css , *.js ) from the specified ${resourceBase}/${request.pathInfo}

resourceHandler not involved in ServletContextHandler , not suitable for mixing with ServletContextHandler .

Also, be sure to set ServletContextHandler.setContextPath(String) to your desired context path (usually "/" )

And yes, you can even have multiple DefaultServlet configurations in one ServletContextHandler .

+7
Feb 09 '15 at 20:51
source share



All Articles