基於內嵌的tomcat7進行開發

  1. 在開發工程的根目錄下建如下目錄(好比個人工程的所在目錄爲jeewx2.0,則建如下目錄java

    jeewx2.0/Embedded/confweb

    jeewx2.0/Embedded/libapache

    jeewx2.0/Embedded/webappstomcat

    jeewx2.0/Embedded/workwebsocket

  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目錄下。app

3. 從對應的tomcat的發行版本(好比本例爲tomcat-7.0.59)的conf目錄下拷貝context.xml和web.xml到jeewx2.0/Embedded/conf目錄下webapp

4. 將jeewx2.0/Embedded/lib目錄下的全部的jar包加到工程的classpath下,若是是用maven,則能夠拷貝如下配置到pom.xml文件:socket

<!-- 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類,代碼以下:maven

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目錄下,不然啓動會出錯(目前還沒找到解決辦法)ide

參考連接:

基於內嵌Tomcat的應用開發

相關文章
相關標籤/搜索