熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

嵌入式Tomcat的實現

2013-11-23 20:07:01  來源: Java開源技術 

  import javaioFile;

  import InetAddress;

  import orgapachecatalinaContext;

  import orgapachecatalinaEngine;

  import orgapachecatalinaHost;

  import orgapachecatalinastartupEmbedded;

  public class EmbeddedTomcat {

  private String contextPath = null;

  private String hostName=null;

  private String catalinaHomePath=null;

  private int    port=;

  private Embedded embedded = null;

  private Host host = null;

  public EmbeddedTomcat(String contextPathString catalinaHomePathString hostNameint port)

  {

  ntextPath=contextPath;

  thiscatalinaHomePath=catalinaHomePath;

  thishostName=hostName;

  thisport=port;

  }

  /**

  * This method Starts the Tomcat server

  */

  public void startTomcat() throws Exception {

  // Create an embedded server

  embedded = new Embedded();

  Engine engine = null;

  // SystemsetProperty(catalinahome getPath());

  embeddedsetCatalinaHome(catalinaHomePath);

  // Create an engine

  engine = embeddedcreateEngine();

  // Create a default virtual host

  host = embeddedcreateHost(hostName contextPath+/webapps);

  engineaddChild(host);

  enginesetDefaultHost(hostgetName());

  // Create the ROOT context

  Context rootCxt = embeddedcreateContext(contextPath + /webapps/ROOT);

  Context manageCxt = embeddedcreateContext(/managercontextPath+/webapps/manager);

  //Create your own context

  Context scoreCxt = embeddedcreateContext(/vmm contextPath+/webapps/vmm);

  rootCxtsetPrivileged(true);

  hostaddChild(rootCxt);

  hostaddChild(manageCxt);

  hostaddChild(scoreCxt);

  // Install the assembled container hierarchy

  embeddedaddEngine(engine);

  // Assemble and install a default HTTP connector

  embeddedaddConnector(embeddedcreateConnector(InetAddressgetByName(null) port false));

  // Start the embedded server

  embeddedstart();

  }

  /**

  * This method Stops the Tomcat server

  */

  public void stopTomcat() throws Exception {

  // Stop the embedded server

  embeddedstop();

  }

  public static void main(String args[]) {

  try {

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

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

  String hostName=localhost;

  int port =;

  Systemoutprintln(contextPath:+contextPath);

  Systemoutprintln(catalinaHomePath:+catalinaHomePath);

  EmbeddedTomcat tomcat = new EmbeddedTomcat(contextPathcatalinaHomePathhostNameport);

  tomcatstartTomcat();

  }

  catch( Exception e ) {

  eprintStackTrace();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27954.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.