Jetty启动应用

配置

pom.xml文件修改

        
            org.eclipse.jetty
            jetty-server
        
        
        
            org.eclipse.jetty
            jetty-webapp
        

Jetty配置文件

建立一个目录jetty/etc,在这个目录下创建jetty.xml




























    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      
      
      
      false
    

    
    
    
    
      
        
      
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      https
      
      
      
      
      
      
      
      512
      
      
    


    
    
    
    
    
    
    
    
    
    
    
    
      
        
         
           
             
           
           
             
           
         
        
      
    

    
    
    
    true
    5000
    
    


建立文件webdefault.xml


 

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

  
    Default web.xml file.  
    This file is applied to a Web application before it's own WEB_INF/web.xml file
  

  
  
  
  
  
   org.eclipse.jetty.servlet.listener.ELContextCleaner
  
  
  
  
  
    
  
   org.eclipse.jetty.servlet.listener.IntrospectorCleaner
  
  

  
  
  
  

  
  
  
  
  
  
 
  
    default
    org.eclipse.jetty.servlet.DefaultServlet
    
      aliases
      false
    
    
      acceptRanges
      true
    
    
      dirAllowed
      true
    
    
      welcomeServlets
      false
    
    
      redirectWelcome
      false
    
    
      maxCacheSize
      256000000
    
    
      maxCachedFileSize
      200000000
    
    
      maxCachedFiles
      2048
    
    
      gzip
      false
    
    
      etags
      false
    
    
      useFileMappedBuffer
      true
    
    
    
    0
  

  
    default
    /
  


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    jsp
    org.eclipse.jetty.jsp.JettyJspServlet
    
      logVerbosityLevel
      DEBUG
    
    
      fork
      false
    
    
      xpoweredBy
      false
    
    
      compilerTargetVM
      1.7
    
    
      compilerSourceVM
      1.7
    
    
    0
  

  
    jsp
    *.jsp
    *.jspf
    *.jspx
    *.xsp
    *.JSP
    *.JSPF
    *.JSPX
    *.XSP
  


  
  
  
  
    30
  

  
  
  
  
  
  
  

  
  
  
  
    index.html
    index.htm
    index.jsp
  

  
  
  
  
    
      ar
      ISO-8859-6
    
    
      be
      ISO-8859-5
    
    
      bg
      ISO-8859-5
    
    
      ca
      ISO-8859-1
    
    
      cs
      ISO-8859-2
    
    
      da
      ISO-8859-1
    
    
      de
      ISO-8859-1
    
    
      el
      ISO-8859-7
    
    
      en
      ISO-8859-1
    
    
      es
      ISO-8859-1
    
    
      et
      ISO-8859-1
    
    
      fi
      ISO-8859-1
    
    
      fr
      ISO-8859-1
    
    
      hr
      ISO-8859-2
    
    
      hu
      ISO-8859-2
    
    
      is
      ISO-8859-1
    
    
      it
      ISO-8859-1
    
    
      iw
      ISO-8859-8
    
    
      ja
      Shift_JIS
    
    
      ko
      EUC-KR
    
    
      lt
      ISO-8859-2
    
    
      lv
      ISO-8859-2
    
    
      mk
      ISO-8859-5
    
    
      nl
      ISO-8859-1
    
    
      no
      ISO-8859-1
    
    
      pl
      ISO-8859-2
    
    
      pt
      ISO-8859-1
    
    
      ro
      ISO-8859-2
    
    
      ru
      ISO-8859-5
    
    
      sh
      ISO-8859-5
    
    
      sk
      ISO-8859-2
    
    
      sl
      ISO-8859-2
    
    
      sq
      ISO-8859-2
    
    
      sr
      ISO-8859-5
    
    
      sv
      ISO-8859-1
    
    
      tr
      ISO-8859-9
    
    
      uk
      ISO-8859-5
    
    
      zh
      GB2312
    
    
      zh_TW
      Big5
    
  

  
  
  
  
    
      Disable TRACE
      /
      TRACE
    
    
  
  
    
      Enable everything but TRACE
      /
      TRACE
    
  


启动应用代码

JettyServer.java


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.xml.sax.SAXException;

public class JettyServer extends Server {
    private String xmlConfigPath;

    private String contextPath;

    private String warPath;

    private String resourceBase = "./src/main/webapp";

    private String webXmlPath = "./src/main/webapp/WEB-INF/web.xml";

    public JettyServer(int port,String xmlConfigPath, String contextPath,
            String resourceBase, String webXmlPath) {
        this(port,xmlConfigPath, contextPath, resourceBase, webXmlPath, null);
    }

    public JettyServer(int port,String xmlConfigPath, String contextPath) {
        this(port,xmlConfigPath, contextPath, null, null, null);
    }

    public JettyServer(int port,String xmlConfigPath, String contextPath,
            String warPath) {
        this(port,xmlConfigPath, contextPath, null, null, warPath);
    }

    public JettyServer(int port,String xmlConfigPath, String contextPath,
            String resourceBase, String webXmlPath, String warPath) {
        super(port);
        if (StringUtils.isNotBlank(xmlConfigPath)) {
            this.xmlConfigPath = xmlConfigPath;
            readXmlConfig();
        }

        if (StringUtils.isNotBlank(warPath)) {
            this.warPath = warPath;
            if (StringUtils.isNotBlank(contextPath)) {
                this.contextPath = contextPath;
                applyHandle(true);
            }
        } else {
            if (StringUtils.isNotBlank(resourceBase))
                this.resourceBase = resourceBase;
            if (StringUtils.isNotBlank(webXmlPath))
                this.webXmlPath = webXmlPath;
            if (StringUtils.isNotBlank(contextPath)) {
                this.contextPath = contextPath;
                applyHandle(false);
            }
        }

    }

    private void readXmlConfig() {
        try {
            XmlConfiguration configuration = new XmlConfiguration(
                    new FileInputStream(this.xmlConfigPath));
            configuration.configure(this);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (SAXException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void applyHandle(Boolean warDeployFlag) {

        ContextHandlerCollection handler = new ContextHandlerCollection();

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath(contextPath);
        webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");

        if (!warDeployFlag) {
            webapp.setResourceBase(resourceBase);
            webapp.setDescriptor(webXmlPath);
        } else {
            webapp.setWar(warPath);
        }

        handler.addHandler(webapp);

        super.setHandler(handler);
    }

    public void startServer() {
        try {
            super.start();
            System.out.println("current thread:"
                    + super.getThreadPool().getThreads() + "| idle thread:"
                    + super.getThreadPool().getIdleThreads());
            super.join();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String getXmlConfigPath() {
        return xmlConfigPath;
    }

    public void setXmlConfigPath(String xmlConfigPath) {
        this.xmlConfigPath = xmlConfigPath;
    }

    public String getContextPath() {
        return contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    public String getResourceBase() {
        return resourceBase;
    }

    public void setResourceBase(String resourceBase) {
        this.resourceBase = resourceBase;
    }

    public String getWebXmlPath() {
        return webXmlPath;
    }

    public void setWebXmlPath(String webXmlPath) {
        this.webXmlPath = webXmlPath;
    }

    public String getWarPath() {
        return warPath;
    }

    public void setWarPath(String warPath) {
        this.warPath = warPath;
    }
}

启动应用的主程序

public class JettyServerStart {
    public static void main(String[] args) {
        JettyServer server = new JettyServer(9090,
                "./jetty/etc/jetty.xml", "/");
        server.startServer();
    }
}

你可能感兴趣的:(Jetty启动应用)