本文介绍了使用Jetty嵌入式容器时,ServletContextHandler.setResourceBase和ResourceHandler.setResourceBase有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用嵌入式Jetty来创建静态网站。 ServletContextHandler.setResourceBase(...)与ResourceHandler.setResourceBase(...)具有相同的效果吗?

I'm using embedded Jetty to create a static web site. Does ServletContextHandler.setResourceBase("...") have a same effect as ResourceHandler.setResourceBase("...")?

示例:

// ServletContextHandler case
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setResourceBase("/tmp/...");
// ResourceHandler case
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase("/tmp/...");

我已经尝试过对它们进行测试。 ResourceHandler正是我想要的。但否则不会。它们之间的区别是什么?

I have tried test both of them. ResourceHandler works exactly what I want. But otherwise doesn't. What's the difference between them?

(对不起,我的英文不好:P)

(Sorry for my poor English :P)

更改后,下面是整个代码。 Context(/)提供静态文件,wsContext(/ ws)提供Web套接字端点。当然,Context(/)也可以为Web套接字端点提供服务。

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.");


推荐答案

使用该设置,将永远不会调用resourceHandler ,因为 DefaultServlet 处理(或 Default404Servlet )结尾 ServletContextHandler 链将始终响应,不允许 resourceHandler 甚至执行。

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.

如果您有 ServletContextHandler ,请不要使用 ResourceHandler 使用 DefaultServlet ServletContextHandler 中设置和提供静态文件。

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

ResourceHandler 非常简单,如果你想要更多的控件/功能,请使用 ServletContextHandler DefaultServlet / code>而不是。

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

好的,不用了...

是 ServletContext 本身用于配置其上下文 resourceBase

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

(注意: setResourceBase()的参数是一个URL字符串,可以指向 file:// 目录,甚至是 jar:file:// 位置。几乎任何支持)

(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} / 是,例如:


    • 字符串getRealPath(字符串路径)

    • URL getResource(String path)

    • InputStream getResourceAsStream(String path)

    • 设置< String> getResources(String path)

    • ${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)

      ResourceHandler 不参加 ServletContextHandler 是不适合与 ServletContextHandler 混合。

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

      此外,不要忘记将 ServletContextHandler.setContextPath(String)设置为所需的上下文路径(通常为/

      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.

      这篇关于使用Jetty嵌入式容器时,ServletContextHandler.setResourceBase和ResourceHandler.setResourceBase有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 19:51