从Alfred文章中扣出来的,以前到是没有用过这种方法,收录收录:):
package com.wisekernel;
import java.net.InetAddress;
import org.apache.catalina.Connector; import org.apache.catalina.Context; import org.apache.catalina.Engine; import org.apache.catalina.Host; import org.apache.catalina.startup.Embedded;
public class MyEmbededTomcat {
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Embedded tomcat = new Embedded(); tomcat.setDebug(9); Engine engine = tomcat.createEngine(); Host host = tomcat.createHost("localhost",""); engine.setDefaultHost("localhost"); engine.addChild(host); Context context = tomcat.createContext("","D:/Program Files/eclipse311/workspace/tuition1/WeBRoot"); host.addChild(context); tomcat.addEngine(engine); Connector connector = tomcat.createConnector((InetAddress)null,8080,false); tomcat.addConnector(connector); tomcat.start(); }
}
package com.wisekernel;
import org.mortbay.http.SocketListener; import org.mortbay.jetty.Server;
public class Jetty5 {
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Server server = new Server(); SocketListener listener = new SocketListener(); listener.setPort(8080); server.addListener(listener); server.addWebApplication("/", "./WebRoot"); server.start(); }
}
EmbededTomcat 的createContext()方法中写入的应该是Web App的绝对路径。这点不如Jetty来的方便,Jetty可以写相对路径。 EmbededTomcat 的项目classpath中不能有XerecsImpl.jar,否则会报错,这个bug不知道怎么fix。
|