为什么使用内嵌Tomcat开发?

开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率.

怎么搭建内嵌Tomcat环境呢?(以tomcat6和tomcat7为例)

在开发工程(xxxx)的根目录下新建以下的目录:
xxxx/Embedded/conf
xxxx/Embedded/lib
xxxx/Embedded/webapps
xxxx/Embedded/work

apache-tomcat-6.0.32

apache官方只提供了apache-tomcat5和apache-tomcat7的内嵌版本,apache-tomcat6是没有的,但我们同样可以提供内嵌版本

1.将apache-tomcat-6.0.32\lib下所有的jar包拷到xxxx/Embedded/lib下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
annotations-api.jar
catalina.jar
catalina-ant.jar
catalina-ha.jar
catalina-tribes.jar
ecj-3.3.1.jar
el-api.jar
jasper.jar
jasper-el.jar
jsp-api.jar
servlet-api.jar
tomcat-coyote.jar
tomcat-dbcp.jar
tomcat-i18n-es.jar
tomcat-i18n-fr.jar
tomcat-i18n-ja.jar

2.将apache-tomcat-6.0.32\bin下的tomcat-juli.jar包拷到xxxx/Embedded/lib下。

3.将JDK安装目录下的lib中的tools.jar包拷到xxxx/Embedded/lib下。

4.将apache-tomcat-6.0.32\conf下的context.xml和web.xml文件拷贝到xxxx/Embedded/conf下。

5.将xxxx/Embedded/lib下的所有Jar包都加到工程的classpath下。

6.在工程中新建EmbeddedTomcat类,该类代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class EmbeddedTomcat {  
private static String CONTEXT_PATH = "/TestForTomcat6";
private static String PROJECT_PATH = System.getProperty("user.dir");
private static String WEB_APP_PATH = PROJECT_PATH + File.separatorChar + "WebRoot";
private static String TOMCAT_HOME = PROJECT_PATH + File.separatorChar + "Embedded" + File.separatorChar + "Tomcat";
private static String ENCODING = "UTF-8";
private static int TOMCAT_PORT = 9080;

// 开始嵌入式Tomcat服务器
public static void startTomcat() throws Exception {
try {
long startTime = System.currentTimeMillis();
Embedded tomcat = new Embedded();
// 设置Tomcat的工作目录:工程根目录/Embedded/Tomcat
tomcat.setCatalinaHome(TOMCAT_HOME);
System.out.println("TOMCAT_HOME:" + TOMCAT_HOME);

// 产生Tomcat引擎
Engine engine = tomcat.createEngine();
//"localhost"对应的是work下的"Catalina"
engine.setName("Catalina");

//创建Host,设置Host的工作目录
Host host = tomcat.createHost("localhost", tomcat.getCatalinaHome());
host.setAutoDeploy(true);

//把Host加入Engine
engine.addChild(host);
engine.setDefaultHost(host.getName());

//创建Context,并加入Host中
Context context = null;
context = tomcat.createContext(CONTEXT_PATH, WEB_APP_PATH);

host.addChild(context);
host.addChild(tomcat.createContext("", ""));
if (!new File(WEB_APP_PATH).exists()) {
System.err.println("请检查应用的目录以及相关资源是否存在.");
}
tomcat.addEngine(engine);

Connector connector = tomcat.createConnector((java.net.InetAddress) null, TOMCAT_PORT, false);
connector.setURIEncoding(ENCODING);
connector.setUseBodyEncodingForURI(true);
tomcat.addConnector(connector);
AprLifecycleListener listener = new AprLifecycleListener();
tomcat.addLifecycleListener(listener);
tomcat.start();
//停住一个小时(3600秒)
Thread.sleep(3600000);
tomcat.stop();
System.err.println("Tomcat started in " + (System.currentTimeMillis() - startTime) + " ms.");
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
try {
EmbeddedTomcat.startTomcat();
} catch (Exception e) {
e.printStackTrace();
}
}


}

apache-tomcat-7.0.22

1.请先到apache-tomcat官方网站下载apache-tomcat-7.0.23-embed内嵌版本,解压后将apache-tomcat-7.0.23-embed下的所有jar包拷贝到xxxx/Embedded/lib下,具体的jar如下:

1
2
3
4
5
6
ecj-3.7.jar
tomcat-dbcp.jar
tomcat-embed-core.jar
tomcat-embed-jasper.jar
tomcat-embed-logging-juli.jar
tomcat-embed-logging-log4j.jar

2.将apache-tomcat-7.0.23/conf下的context.xml和web.xml文件拷贝到xxxx/Embedded/conf下。

3.将xxxx/Embedded/lib下的所有Jar包都加到工程的classpath下。

4.在工程中新建EmbeddedTomcat类,该类代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class EmbededTomcat {  
private final Log log=LogFactory.getLog(getClass());
private static String CONTEXT_PATH = "/TestForTomcat7";
private static String PROJECT_PATH = System.getProperty("user.dir");
private static String WEB_APP_PATH = PROJECT_PATH + File.separatorChar + "WebRoot";
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(7080);
embededTomcat.start();
}
}

遇到问题怎么办?

1.内嵌Tomcat启动时报OutOfMemoryError异常,如何设置JVM的内存大小?

答:请按照以下步骤设置JVM的内存大小:

(1)点击“Window->Preferences->Java->Installed JREs”可以看到所有的JRE。

(2)点中当前使用的JRE,再点击右边的“Edit”。

(3)为该JRE的“Default VM Arguments”配置参数值:-Xmx1024M -Xms512M -XX:MaxPermSize=256m。

2.如何配置数据库的JNDI数据源?

答:打开XXXX\Embedded\Tomcat\conf\context.xml配置文件,添加节点的属性来新建数据源配置,如下:

1
2
3
4
5
<Resource name="xxxxDataSource" auth="Container"  
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:xxxxx"
username="xxxx" password="xxxx" maxActive="15" maxIdle="10" initialSize="2"
testWhileIdle="true" validationQuery="select 1 from dual" testOnBorrow="true"/>

来源:http://chenjc-it.iteye.com/blog/1370986