Java uses Jetty to implement an embedded servlet container

Recently, I encountered a problem about jetty in the project, so I did some popular science on the Internet, and then I will share with you:
Jetty is an open source servlet container implemented in Java, which can be used as a complete web server like Tomcat. And Servlet container, but also can be embedded in Java application, call Jetty in Java program.
Jetty download address, the latest version at the time of writing this article is 9.1.2, download jetty-distribution-9.1.2.v20140210.zip:
http://download.eclipse.org/jetty/stable-9/dist/
Note that Jetty 9 requires JDK 7, if using JDK 6, there will be an error: java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0.

To implement a servlet container in a java project, you first need to import the following jar package:
jetty-servlet-9.1.2.v20140210.jar
jetty-webapp-9.1.2.v20140210.jar
jetty-xml-9.1.2.v20140210.jar The jar package in
jetty-security-9.1.2.v20140210.jar
lib/jsp folder is the imported war package file in
my project, with simple code attached:

    public static void main(String[] args) throws Exception  
    {  
        Server server = new Server(8080);  
        //声明上下文对象
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath("/");
        webAppContext.setWar("CRweb.war");
        webAppContext.setInitParameter("dirAllowed", "false");  
        //HandlerList顺序执行handler
        HandlerList handlers = new HandlerList();

        //声明ServletContextHandler
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);  
        context.setContextPath("/s"); 
         //设置句柄
        server.setHandler(context);  
        context.addServlet(new ServletHolder(new SelectPatientServlet()), "/selectOne");  

        //处理程序
        handlers.addHandler(context);  
        handlers.addHandler(webAppContext);
        // Add the handlers to the server adminand start jetty.
        server.setHandler(handlers);

        //启动服务star
        server.start();  
    {
    GUI界面界面加载的一些程序
    }
       // 最后加入服务器
        server.join();
    }  

The project involves embedding browser plug-ins in Java, and I will give you a brief introduction in the next document.
For details about jetty's implementation of embedded Web server and Servlet container, see the original text: http://blog.csdn.net/xiao__gui/article/details/20691363

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325708000&siteId=291194637