Jetty 9: Setting up handlers and connectors

孤人 提交于 2020-01-13 04:37:09

问题


I've looked at the documentation for Jetty 9 on the architecture (http://www.eclipse.org/jetty/documentation/current/architecture.html) but I am still confused about the relationship between handlers and connectors.

  1. Can you link a handler to a specific connector (if so, how? The connector doesn't seem to have a setHandler method)?

  2. Or does everything go to a main handler and then you distribute things from there? (i.e. You figure out form what connector it came from so then you forward it to a different handler or handle it yourself)

Thanks a lot!


回答1:


Connectors are the components that listen for incoming connections.

Handlers are the low level jetty mechanism used to handle all requests.

Jetty sends all valid requests (there are a class of requests that are just bad HTTP usage and can result in things like a 400 Bad Request) to whatever is registered at Server.getHandler()

There are many types of function specific handlers, pick one that best suits your needs and extend from it, or wrap your handler around a more generalized approach.

A typical server is setup to have either a HandlerList or HandlerCollection to indicate a list of possible behavior.

Each handler is hit (in order) and if that handler decides it wants to do something it can.

If a handler actually produced something, then a call to baseRequest.setHandled(true); is used to tell Jetty to not process any more handlers after this current one.

As to how to restrict certain handlers to certain connectors, that's done via the virtualhosts mechanism.

VirtualHosts is a concept baked into the ContextHandler specific handlers, so you'll want to wrap your custom handlers in a ContextHandler to get the benefit of VirtualHosts.

To use this, you would name your connector using Connector.setName(String) and then use the @{name} syntax on the VirtualHosts definition of the ContextHandler to specific that only that named connector can be used to serve that specific ContextHandler.

Example:

    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/jetty-9-setting-up-handlers-and-connectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!