I am trying to configure embedded Jetty (7.6) to handle the WebSocketHandler, ServletContextHandler and ResourceHandler classes at the same time.
I have tried using both HandlerCollection and HandlerList classes but I can't get all 3 parts to work.
Server server = new Server(8081);
// static files handler
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(true);
resourceHandler.setResourceBase("./src/main/webapp/");
// servlet handler
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(new ServletHolder(new myServlet()), "/myServlet");
// websocket handler
myWebSocketHandler myWebSocketHandler = new myWebSocketHandler();
// putting it together
HandlerCollection handlerList = new HandlerCollection();
handlerList.setHandlers(new Handler[]{resourceHandler,myWebSocketHandler,servletContextHandler});
server.setHandler(handlerList);
In this state, the static files and the servlet are handled fine, but when I send a websocket request, I get:
2012-02-22 10:16:44.703:WARN:oejs.Response:Committed before 503 null
2012-02-22 10:16:44.705:WARN:oejs.AbstractHttpConnection:/
java.lang.IllegalStateException: Committed
at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1080)
...
and if I check the state of the base requests "handled" property inside my websockets handler, it's already set to true. So this means that my request is being handled and committing before it gets to myWebSocketHandler?
Any comments are appreciated, thanks.
Have you tried simply switching the order of the handlers?
handlerList.setHandlers(new Handler[]{myWebSocketHandler,servletContextHandler,resourceHandler});
Jetty runs the handlers in the order you add them, and the resource handler handles every request that comes its way (potentially by serving a 404
error).
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