嵌入式Tomcat的实现

导读:
  import java.io.File;
  import java.net.InetAddress;
  import org.apache.catalina.Context;
  import org.apache.catalina.Engine;
  import org.apache.catalina.Host;
  import org.apache.catalina.startup.Embedded;
  public class EmbeddedTomcat {
  private String contextPath = null;
  private String hostName=null;
  private String catalinaHomePath=null;
  private int port=8080;
  private Embedded embedded = null;
  private Host host = null;
  public EmbeddedTomcat(String contextPath,String catalinaHomePath,String hostName,int port)
  {
  this.contextPath=contextPath;
  this.catalinaHomePath=catalinaHomePath;
  this.hostName=hostName;
  this.port=port;
  }
  /**
  * This method Starts the Tomcat server.
  */
  public void startTomcat() throws Exception {
  // Create an embedded server
  embedded = new Embedded();
  Engine engine = null;
  // System.setProperty("catalina.home", getPath());
  embedded.setCatalinaHome(catalinaHomePath);
  // Create an engine
  engine = embedded.createEngine();
  // Create a default virtual host
  host = embedded.createHost(hostName, contextPath+"/webapps");
engine.setDefaultHost(host.getName());

        // Create the ROOT context


        Context rootCxt = embedded.createContext("",contextPath + "/webapps/ROOT");


        Context manageCxt = embedded.createContext("/manager",contextPath+"/webapps/manager");


        //Create your own context


        Context scoreCxt = embedded.createContext("/vmm", contextPath+"/webapps/vmm");


        rootCxt.setPrivileged(true);


        host.addChild(rootCxt);


        host.addChild(manageCxt);


        host.addChild(scoreCxt);


        // Install the assembled container hierarchy


        embedded.addEngine(engine);


        // Assemble and install a default HTTP connector


        embedded.addConnector(embedded.createConnector(InetAddress.getByName(null), port, false));


        // Start the embedded server


        embedded.start();


      }


      /**


        * This method Stops the Tomcat server.


        */


      public void stopTomcat() throws Exception {


        // Stop the embedded server


        embedded.stop();


      }


      public static void main(String args[]) {


        try {


          String contextPath=(new File(".")).getCanonicalPath();


          String catalinaHomePath =(new File("./conf/tomcat")).getCanonicalPath();


          String hostName="localhost";


          int port =80;


          System.out.println("contextPath:"+contextPath);


          System.out.println("catalinaHomePath:"+catalinaHomePath);


          EmbeddedTomcat tomcat = new EmbeddedTomcat(contextPath,catalinaHomePath,hostName,port);


          tomcat.startTomcat();


        }


        catch( Exception e ) {


          e.printStackTrace();


        }


      }


    }


本文转自
http://java.chinaitlab.com/Tomcat/755514.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值