基于内嵌的tomcat7进行开发

  1. 在开发工程的根目录下建以下目录(比如我的工程的所在目录为jeewx2.0,则建以下目录

    jeewx2.0/Embedded/conf

    jeewx2.0/Embedded/lib

    jeewx2.0/Embedded/webapps

    jeewx2.0/Embedded/work

  2. 从apahce tomcat网站下载embed tomcat所需要的所有的jar包:http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.59/bin/embed/apache-tomcat-7.0.59-embed.zip,解压后把所有的jar包解压到jeewx2.0/Embedded/lib目录下。

3. 从对应的tomcat的发行版本(比如本例为tomcat-7.0.59)的conf目录下拷贝context.xml和web.xml到jeewx2.0/Embedded/conf目录下

4. 将jeewx2.0/Embedded/lib目录下的所有的jar包加到工程的classpath下,如果是用maven,则可以拷贝以下配置到pom.xml文件:

<!-- embed tomcat -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-core</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-logging-juli</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-logging-log4j</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-websocket</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-el</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-dbcp</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>

在工程中新建一个EmbededTomcat类,代码如下:

package org.jeecgframework.tools;

import java.io.File;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EmbededTomcat {
	private final Log log=LogFactory.getLog(getClass());
	private static String CONTEXT_PATH = "/jeewx";
	private static String PROJECT_PATH = System.getProperty("user.dir");
	private static String WEB_APP_PATH = PROJECT_PATH + File.separatorChar + "target/jeewx";
	private static String CATALINA_HOME = PROJECT_PATH + "/Embedded/Tomcat";
	private Tomcat tomcat = new Tomcat();
    private int port;
	
    public EmbededTomcat(int port){
        this.port=port;
    }
    
	public void start()throws Exception{
		tomcat.setPort(port);
		tomcat.setBaseDir(CATALINA_HOME);
		tomcat.getHost().setAppBase(WEB_APP_PATH);
		try{
			StandardServer server = (StandardServer)tomcat.getServer();
		    AprLifecycleListener listener = new AprLifecycleListener();
		    server.addLifecycleListener(listener);
			tomcat.addWebapp(CONTEXT_PATH, WEB_APP_PATH);
		}catch(ServletException e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		try{
			tomcat.start();
			tomcat.getServer().await();
		}catch(LifecycleException e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		log.info("Tomcat started.");
	}
	
	public void stop()throws Exception{
            try{
                tomcat.stop();
            }
            catch(LifecycleException ex){
                ex.printStackTrace();
                log.error(ex.getMessage());
                throw ex;
            }
            log.info("Tomcat stoped");
	}
        
    public void setPort(int port){
        this.port=port;
    }
    public int getPort(){
        return this.port;
    }
    
    public static void main(String[] args) throws Exception {
    	EmbededTomcat embededTomcat = new EmbededTomcat(8080);
    	embededTomcat.start();
	}
}

其中WEB_APP_PATH是web目录,如果是maven工程,不能指向到src/main/webapp目录,只能指向到target/jeewx目录下,否则启动会出错(目前还没找到解决办法)

参考链接:

基于内嵌Tomcat的应用开发

你可能感兴趣的:(java,tomcat,Web,embbed)