Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

java - What is difference between ServletContextHandler.setResourceBase and ResourceHandler.setResourceBase when using Jetty embedded container?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With that setup, the resourceHandler will never be called, as the DefaultServlet processing (or Default404Servlet) at the end of the ServletContextHandler chain will always respond, not allowing resourceHandler to even execute.

If you have a ServletContextHandler, do not use ResourceHandler use the DefaultServlet in that ServletContextHandler to setup and serve your static files.

ResourceHandler is very simplistic, if you want more control / features, use a DefaultServlet configured in your ServletContextHandler instead.

Ok, with that out of the way ...

The ServletContextHandler.setBaseResource(Resource) is the place for the ServletContext itself to configure its context-wide resourceBase.

(Note: the parameter of setResourceBase() is a URL string that can point to a file:// directory or even a jar:file:// location. Pretty much anything supported by Resource.newResource(String))

  • ${resourceBase}/ is the lookup point for various methods in javax.servlet.ServletContext such as:
    • String getRealPath(String path)
    • URL getResource(String path)
    • InputStream getResourceAsStream(String path)
    • Set<String> getResources(String path)
  • Requested resources that don't match any of your servlets or filters, will then be handled by the DefaultServlet, which might serve static resources (such as *.html, *.css, *.js) from the specified ${resourceBase}/${request.pathInfo}

ResourceHandler does not participate in ServletContextHandler is an is inappropriate to mix with ServletContextHandler.

Also, don't forget to set ServletContextHandler.setContextPath(String) to your desired context path (usually "/")

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...