Jetty 9: Setting up handlers and connectors

[亡魂溺海] 提交于 2019-12-04 14:42:09

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