I studied online yesterday about embedding Tomcat in the main program, rather than running a Web project copy into Tomcat as before. The reason for this is that because the project is deployed to the client side, it needs to be updated manually when the update is made, then copy the code to tomcat and then run it. With embeded Tomcat, you can separate the project from Tomcat and, when updating, use a custom program to automate the update, and then start Tomcat (or other Java EE containers) to run the project before the update is complete.
The ultimate effect of this is to modify the way the project is run. The original operation was a Tomcat-centric, Tomcat-initiated and terminated project, which is now centered on our startup program and is responsible for starting and terminating the project. Just like the popular CS program, there are separate startup scripts, pre-initialization of the environment at startup, updates and other operations, and the final project starts after completion.
This article mainly explains how to use embeded tomcat to start and terminate in code. The general articles on the Internet are tomca5.x to do, the use of the latest TOMCAT7, because TOMCAT7 for embeded development, a separate org.apache.tomcat.embed package to carry out independent embed development. The following is the appropriate MAVEN package
0102030405060708091011121314151617181920 |
<
dependency
>
<
groupId
>org.apache.tomcat.embed</
groupId
>
<
artifactId
>tomcat-embed-core</
artifactId
>
<
version
>7.0.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tomcat</
groupId
>
<
artifactId
>tomcat-util</
artifactId
>
<
version
>7.0.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tomcat.embed</
groupId
>
<
artifactId
>tomcat-embed-jasper</
artifactId
>
<
version
>7.0.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tomcat.embed</
groupId
>
<
artifactId
>tomcat-embed-logging-juli</
artifactId
>
<
version
>7.0.2</
version
>
</
dependency
>
|
The core package in the embed package is used, as well as the Jasper package for compiling the JSP, followed by the tool class and the Logging-juli package for the play record. Start writing code:
1234567 |
//设置工作目录
String catalina_home =
"d:/"
;
Tomcat tomcat =
new Tomcat();
tomcat.setHostname(
"localhost"
);
tomcat.setPort(startPort);
//设置工作目录,其实没什么用,tomcat需要使用这个目录进行写一些东西 , Courier, monospace!important; Font-weight:normal!important; Font-style:normal!important |
Embedded Java EE development with embeded Tomcat-start Tomcat