Jetty 9: configuring handlers and connectors

I have looked at the documentation for Jetty 9 on architecture ( ) but I am still confused about the relationship between handlers and sockets.

  • Can you bind a handler to a specific connector (if so, how? The connector doesn't have a setHandler method)?

  • Or does everything go to the main handler and then you distribute things from there? (i.e. you determine which connector it came from and then forward it to another handler or handle it yourself).

Thank you so much!


source to share

1 answer


are components that listen for incoming connections.


is a low level quay mechanism used to handle all requests.

Jetty sends all valid requests (there is a class of requests that are just bad use of HTTP and can lead to things like 400 Bad Request

) to what is registered inServer.getHandler()

There are many types of custom function handlers, choose the one that best suits your needs and extends from it, or wrap the handler around a more generalized approach.

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

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

If the handler actually did something, then the call is baseRequest.setHandled(true);

used to tell Jetty not to process any more handlers after this current one.

How to restrict some handlers to some connectors that are executed using the virtualhosts mechanism.

VirtualHosts is a concept baked into custom handlers ContextHandler

, so you'll want to wrap your custom handlers in ContextHandler

to take advantage of VirtualHosts.

To use this, you must name your connector with Connector.setName(String)

, and then use the syntax @{name}

in your VirtualHosts definition ContextHandler

to indicate that only that named connector can be used to serve that particular one ContextHandler



    ServerConnector httpConnector = new ServerConnector(server);
    httpConnector.setName("unsecured"); // named connector

    ContextHandler helloHandler = new ContextHandler();
    helloHandler.setHandler(new HelloHandler("Hello World"));
    helloHandler.setVirtualHosts(new String[]{"@unsecured"});




All Articles