usp
usp

Reputation: 43

Embedded Jetty with WebAppContext and ServletContextHandler and other Handlers

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();
}

}

Upvotes: 2

Views: 3070

Answers (1)

Willempie
Willempie

Reputation: 103

I know it's a late post. But I had the same question and I found an answer so I want to share it with anyone who might be searching. The answer is very simple: Just add the servlet to the WebAppContext.

public class JettyTest {
public static void main(String[] args) throws Exception {
    Server server = new Server(8081);

    ContextHandlerCollection contexts = new ContextHandlerCollection();



    ServletHolder mainHolder = new ServletHolder(new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
                throws ServletException, IOException {
            resp.getWriter().write("main");
        }
    });

    WebAppContext webAppContext = new WebAppContext("test.war", "/test");
    webAppContext.addServlet(mainHolder, "/main");

    server.setHandler(webAppContext);

    server.start();
    server.join();
}

Now the servlet can be found in anydomain:8081/test/main. I don't know how to get '/main' under root :)

Upvotes: 0

Related Questions