美文网首页
在项目中内嵌jetty web server

在项目中内嵌jetty web server

作者: 大侠陈 | 来源:发表于2020-02-01 10:53 被阅读0次

    在maven中添加依赖

            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-server</artifactId>
                <version>9.1.4.v20140401</version>
            </dependency>
    
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-webapp</artifactId>
                <version>9.1.4.v20140401</version>
            </dependency>
    
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-continuation</artifactId>
                <version>9.1.4.v20140401</version>
            </dependency>
    
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-jsp</artifactId>
                <version>9.1.4.v20140401</version>
            </dependency>
    

    运行示例代码

    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

          本文链接:https://www.haomeiwen.com/subject/ffitxhtx.html