我看过Jetty9ontheArchitecture (http://www.eclipse.org/jetty/documentation/current/architecture.html)的文档,但我仍然对处理程序和连接器之间的关系感到困惑。
非常感谢!
发布于 2014-10-01 19:39:16
Connectors是侦听传入连接的组件。
Handlers是用于处理所有请求的低级jetty机制。
Jetty将所有有效的请求(有一类只会导致400 Bad Request之类的错误HTTP使用的请求)发送到在Server.getHandler()上注册的任何东西
有许多类型的特定于函数的处理程序,选择最适合您的需要的处理程序并从其中进行扩展,或者将处理程序包装在更通用的方法上。
典型的服务器设置为具有HandlerList或HandlerCollection,以指示可能的行为列表。
每个处理程序都会被击中(按顺序),如果该处理程序决定它想要做一些它可以做的事情。
如果一个处理程序实际产生了什么,那么调用baseRequest.setHandled(true);就会告诉Jetty在这个当前的处理程序之后不再处理任何处理程序。
至于如何将某些处理程序限制在特定的连接器上,这是通过虚拟主机机制完成的。
VirtualHosts是ContextHandler特定处理程序中的一个概念,因此您需要将自定义处理程序包装在ContextHandler中,以获得VirtualHosts的好处。
要使用此方法,您可以使用Connector.setName(String)命名连接器,然后在ContextHandler的VirtualHosts定义上使用@{name}语法来指定只有命名的连接器才能服务于特定的ContextHandler。
示例:
ServerConnector httpConnector = new ServerConnector(server);
httpConnector.setName("unsecured"); // named connector
httpConnector.setPort(80);
ContextHandler helloHandler = new ContextHandler();
helloHandler.setContextPath("/hello");
helloHandler.setHandler(new HelloHandler("Hello World"));
helloHandler.setVirtualHosts(new String[]{"@unsecured"});https://stackoverflow.com/questions/26148418
复制相似问题