While it *is* possible to have a WebSocketHandler mixed with a WebAppContext, its not really the ideal setup.
First, here's how your specific scenario is setup ...
package jetty.websocket;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.webapp.WebAppContext;
public class MyEmbeddedServer
{
public static void main(String[] args)
{
Server server = new Server(8080);
HandlerList handlers = new HandlerList();
server.setHandler(handlers);
// Add websocket handler
ContextHandler wshandler = new ContextHandler("/ws");
wshandler.setHandler(new MyHandler());
handlers.addHandler(wshandler);
// Add web app
WebAppContext webapp = new WebAppContext();
webapp.setWar("src/test/wars/webapp-b.war");
webapp.setContextPath("/app");
handlers.addHandler(webapp);
// Add default handler (for errors and whatnot)
handlers.addHandler(new DefaultHandler());
// Lets see how the server is setup after it is started
server.setDumpAfterStart(true);
try
{
// Start the server thread
server.start();
// Wait for the server thread to end
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
}
}
End result of this is ...
Any other requests, such as ...
The biggest problem with this is that your WebSocketHandler and the WebAppContext cannot talk to each other directly, as they are in different/isolated classloaders.
Why not just put your websocket class into the webapp itself?
Its far easier, and you suffer no impacts on performance.
Just use a WebSocketServlet instead of a WebSocketHandler, the rest of the code should be identical.
Or, you can use the javax.websocket.* standard to implement websocket within your webapp.
(Have to use Jetty 9.2+ for that tho)