嵌入式商城 嵌入式Tomcat的实现
嵌入式Tomcat的实现
import java io File;
import InetAddress;
import apache catalina Context;
import apache catalina Engine;
import apache catalina Host;
import apache catalina startup Embedded;
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 contextPath String catalinaHomePath String hostName int port)
{
ntextPath=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 addChild(host);
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 = ;
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();
}
}
lishixinzhi/Article/program/Java/ky/201311/27954