在项目中内嵌jetty web server

在maven中添加依赖

        
            org.eclipse.jetty
            jetty-server
            9.1.4.v20140401
        

        
            org.eclipse.jetty
            jetty-webapp
            9.1.4.v20140401
        

        
            org.eclipse.jetty
            jetty-continuation
            9.1.4.v20140401
        

        
            org.eclipse.jetty
            jetty-jsp
            9.1.4.v20140401
        

运行示例代码

public class Main {
    public static class HelloServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
            resp.getWriter().println("hello world");
        }
    }
    public static void main( String[] arg) throws Exception {
        Server server = new Server(8111);


        ServletContextHandler servletContextHandler = new ServletContextHandler();
        servletContextHandler.setContextPath("/");
        servletContextHandler.addServlet(HelloServlet.class, "/hello");

        HandlerList handlerList = new HandlerList();
        handlerList.setHandlers(new Handler[]{servletContextHandler});
        server.setHandler(handlerList);

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

你可能感兴趣的:(在项目中内嵌jetty web server)