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包:
http://vefan.iteye.com/upload/attachment/127086/9fb367cf-c62a-3db0-a04b-12fa52ccc9ed.jpg
在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>
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com