I an using embedded Jetty server in my java application. I want to use a web application (war file) and a Servlet in embedded jetty server dynamically at the same time. For that I am using WebAppContext and ServletContextHandler provided in jetty libraries. Once sever started, only servlet can be accessed through the browser. Can anyone give the correct way of using both at once ?
In addition to above two, I want to add WebSocketHandler, CometDServlet, and another static web site as well. Appreciate if anyone can tell me the correct way of doing this.
import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.handler.ContextHandlerCollection;import org.eclipse.jetty.servlet.ServletContextHandler;import org.eclipse.jetty.servlet.ServletHolder;import org.eclipse.jetty.webapp.WebAppContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class JettyTest {public static void main(String[] args) throws Exception { Server server = new Server(8081); ContextHandlerCollection contexts = new ContextHandlerCollection(); ServletContextHandler main = new ServletContextHandler(server, "/", true, false); main.addServlet(new ServletHolder(new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("main"); } }), "/main"); WebAppContext webAppContext = new WebAppContext("test.war", "/test"); contexts.addHandler(webAppContext); contexts.addHandler(main); server.setHandler(contexts); server.start(); server.join();}
}