Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding different handler in single Jetty server

I need a jetty server with multiple servletHandler.

HTTPservlet:

    ServletHandler servletHandler = new ServletHandler();
    server.setHandler(servletHandler);
    servletHandler.addServletWithMapping("com.realtime.webserver.MyServlet", "/MyServlet");

WebsocketServlet:

MyWebSocketHandler myWebSocketHandler = new MyWebSocketHandler ();
             myWebSocketHandler.setHandler(new DefaultHandler());
             server.setHandler(myWebSocketHandler);
             server.start();

I need both should be in single server. Is there any possibilities?

like image 943
Prasath Avatar asked Jun 17 '13 13:06

Prasath


People also ask

What is Handler in Jetty?

In Jetty, Handlers handle requests that are coming through Connectors. One of the Handlers, specifically ServletHandler , allows Jetty to (mostly) support servlets. Servlet is a portable Java EE concept, so you can design your application in a more portable way if you use servlets in Jetty.

How Jetty server works?

The Jetty Server is the plumbing between a collection of Connectors that accept HTTP connections, and a collection of Handlers that service requests from the connections and produce responses, with the work being done by threads taken from a thread pool.


2 Answers

You can use org.eclipse.jetty.server.handler.HandlerCollection (Jetty 9)

HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(new Handler[] {servletHandler, myWebSocketHandler});

Later add handlers to the collection:

handlerCollection.addHandler(newHandler);

Finally,

server.setHandler(handlerCollection);
server.start();
like image 65
Amila Avatar answered Sep 20 '22 08:09

Amila


http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java

This is an example of using many handlers at once on the same server.

Eventually it will be added to the documentation here:

http://www.eclipse.org/jetty/documentation/current/embedded-examples.html

Until that time there are many other examples there that should help make things clearer as well.

like image 42
jesse mcconnell Avatar answered Sep 20 '22 08:09

jesse mcconnell