嵌入式Tomcat

package com.iman.opm.web.common;

import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Embedded;
import java.net.InetAddress;

/**
 * 嵌入式TOMCAT
 */
public class EmbeddedTomcat {

	private String webrootPath = "E:\\workspace\\opm\\WebRoot"; // WEB应用程序路径
	private String contextPath = "/nrms"; // WEB上下文名称
    private int tomcatPort = 80;//TOMCAT端口
	private boolean reloadable = true; // 是否允许热交换class

	private Embedded tomcat; // 嵌入式TOMCAT

	public EmbeddedTomcat() {
		if (System.getProperty("webroot.path") != null) {
			webrootPath = System.getProperty("webroot.path");
		}
		if (System.getProperty("context.path") != null) {
			contextPath = System.getProperty("context.path");
		}
		if (System.getProperty("reloadable") != null) {
			reloadable = Boolean.valueOf(System.getProperty("reloadable"));
		}
		if (System.getProperty("tomcat.control.port") != null) {
			tomcatPort = Integer.parseInt(System.getProperty("tomcat.control.port"));
		}		
	}

	/**
	 * 启动TOMCAT
	 */
	public void startup() throws Exception {
		System.out.println("================ the tomact is starting, please waiting .......");
		tomcat = new Embedded();
		Engine engine = tomcat.createEngine();
		tomcat.setCatalinaHome(webrootPath);

		Host host = tomcat.createHost("localhost", webrootPath);
		Context context = tomcat.createContext(contextPath, webrootPath);
		if (reloadable)
			context.setReloadable(true);
		host.addChild(context);
		engine.addChild(host);
		engine.setDefaultHost(host.getName());

		engine.setName("EmbeddedServer");
		tomcat.addEngine(engine);
		Connector connector = tomcat.createConnector(InetAddress.getByName("0.0.0.0"), tomcatPort, false);
		connector.setURIEncoding("GBK");
		tomcat.addConnector(connector);
		tomcat.start();
	}

	/**
	 * 终止TOMCAT
	 */
	public void shutdown() throws Exception {
		tomcat.stop();
	}

	public static void main(String[] args) {
		try {
			new EmbeddedTomcat().startup();
			System.out.println("================ the tomact has started .......");
		} catch (Exception e) {
			System.out.println("================ the tomact is failed to start .......");
			e.printStackTrace();
		}
	}
}


引入Tomcat相关jar包:



在WebRoot目录下面建立conf目录:
conf目录放置tomcat的三个配置文件:
1、context.xml
2、tomcat-users.xml
3、web.xml

context.xml内容:
	<!--
		The contents of this file will be loaded for each web application
	-->
<Context privileged='true'>

	<!-- Default set of monitored resources -->
	<WatchedResource>WEB-INF/web.xml</WatchedResource>

	<Resource name="jdbc/nrms_ds"
              auth="Container"
              type="javax.sql.DataSource"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@192.168.0.204:1521:xcom"
              username="t_nrms"
              password="t_nrms"
              maxActive="20"
              maxIdle="30"
              maxWait="10000"/>
              
	<Realm className="org.apache.catalina.realm.MemoryRealm"/>
</Context>


tomcat-users.xml内容:
<!--
  NOTE:  By default, no user is included in the "manager" role required
  to operate the "/manager" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<tomcat-users>
  <user name="system" password="system" roles="SecurityUsers" />
  <user name="sysadm" password="sysadm" roles="SecurityUsers" />
</tomcat-users>


你可能感兴趣的:(apache,tomcat,Web,xml,嵌入式)