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?
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With