java Jetty 中的 ServletHandler 和 ServletContextHandler 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30733910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between a ServletHandler and a ServletContextHandler in Jetty?
提问by elanh
I'm trying to get started with an embedded Jetty server. I just want to map requests to different servlets based on the request path.
我正在尝试开始使用嵌入式 Jetty 服务器。我只想根据请求路径将请求映射到不同的 servlet。
What's the difference between creating a ServletHandler
and adding servlets to it as opposed to creating a ServletContextHandler
and adding servlets to that?
创建一个ServletHandler
并向其添加 servlet 与创建一个并向其添加 servlet之间有什么区别ServletContextHandler
?
For example:
例如:
//how is this different...
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(MyServlet.class, "/path");
//from this?
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.addServlet(MyServlet.class, "/path");
回答by Joakim Erdfelt
Most Servlet's require a javax.servlet.ServletContext
object to operate properly.
大多数 Servlet 需要一个javax.servlet.ServletContext
对象才能正常运行。
Using a ServletContextHandler
will create and manage the common ServletContext for all of the Servlets, Filters, Sessions, Security, etc within that ServletContextHandler
. This includes proper initialization, load order, and destruction of the components affected by a ServletContext
as well.
使用 aServletContextHandler
将为该中的所有 Servlet、过滤器、会话、安全等创建和管理公共 ServletContext ServletContextHandler
。这包括正确的初始化、加载顺序和受 a 影响的组件的销毁ServletContext
。
Also note, that ServletHandler
is considered an internal class of ServletContextHandler
and is not meant to be used "in the raw" like that with Jetty. While it is technically possible, it is discouraged for all but the most naive and simplistic implementations of a Servlet.
另请注意,这ServletHandler
被认为是 的内部类,ServletContextHandler
并不意味着像 Jetty 那样在“原始”中使用。虽然它在技术上是可行的,但除了 Servlet 的最幼稚和简单的实现之外,不鼓励它。
回答by ceeleP
For Example you can create VirtualHosts with ServletContextHandler and you can management context easily. That means different context handlers on different ports.
例如,您可以使用 ServletContextHandler 创建 VirtualHosts,并且您可以轻松管理上下文。这意味着不同端口上的不同上下文处理程序。
Server server = new Server();
ServerConnector pContext = new ServerConnector(server);
pContext.setPort(8080);
pContext.setName("Public");
ServerConnector localConn = new ServerConnector(server);
localConn.setPort(9090);
localConn.setName("Local");
ServletContextHandler publicContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
publicContext.setContextPath("/");
ServletHolder sh = new ServletHolder(new HttpServletDispatcher()); sh.setInitParameter("javax.ws.rs.Application", "ServiceListPublic");
publicContext.addServlet(sh, "/*");
publicContext.setVirtualHosts(new String[]{"@Public"});
ServletContextHandler localContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
localContext .setContextPath("/");
ServletHolder shl = new ServletHolder(new HttpServletDispatcher()); shl.setInitParameter("javax.ws.rs.Application", "ServiceListLocal");
localContext.addServlet(shl, "/*");
localContext.setVirtualHosts(new String[]{"@Local"}); //see localConn.SetName
HandlerCollection collection = new HandlerCollection();
collection.addHandler(publicContext);
collection.addHandler(localContext);
server.setHandler(collection);
server.addConnector(pContext);
server.addConnector(localContext);