嵌入式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包:
[img]/upload/attachment/127086/9fb367cf-c62a-3db0-a04b-12fa52ccc9ed.jpg[/img]


在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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值