Original: http://www.blogcn.com/User8/flier_lu/blog/4740621.html When developing Tomcat-based web programs, a comparative headache is how to connect with the front page with the front page with the front page separation. . Although it is possible to support IDE-centric operational environment, it is more difficult to configure trouble, a lot of speed and a lot of speed, and it is better to directly debug the background program directly. A better solution is to use Tomcat's embedded version to embed Tomcat in turn into the background service, and debugging in the background service. In this way, Tomcat has changed from the overall container to the background service. Under the premise of do not change the behavior, it is possible to set the debug environment. For example, a background service debug environment developed by the author, supports the debug command based on the command line, directly controls various background services, and greatly alleviates the debugging difficulty in the integration through the front interface verification results. Although ordinary configuration Tomcat can also be directly embedded in the background program, but recommending the Tomcat custom Embeded version, so the integration is higher and the performance is better. At the same time, because the code is exactly the same, there is no problem of functional differences in the debug environment. Tomcat 5.0.28 Embed tar.gz After extracting the Tomcat Embed version, add all the .jar files in the LIB directory to the ClassPath in the Java project, you can start Tomcat in the Java program. Similar to ordinary Tomcat configuration, it is running requires the structure of the following organization:
Java code:
<
CONTEXT />
Host>
Engine>
Service>
Server>
It is only to configure web.xml, and is done in the embedded version. The first is to establish a Tomcat server and specify its run directory. This directory is best with Tomcat Embed version path.
Java code:
Embedded Tomcat =
New Embedded
(
);
Tomcat.
SetCataLinaHome
(PATH
);
Tomcat.
Addengine
(ENGINE
);
Tomcat.
SetDebug
Logger.
Warning
);
Then create default engine and host and add Host to Engine. The name here only acts as a tag, but the path to Host is best consistent with the Tomcat path. The same Engine is actually possible with multiple virtual hosts, and the automatic test of large sites can be separated.
Java code:
Engine Engine = Tomcat.
CreateEngine
(
);
ENGINE.
SetName
("ESPSERVER"
);
Host host = Tomcat.
Createhost
("Localhost", Tomcat.
GetCataLinaHome
(
"/ WebApps"
);
ENGINE.
AddChild
(Host)
);
ENGINE.
SetDefaulthost
Host.
GetName
(
)
);
Fill in Host's content is actually the establishment of the environment of the specific web application. First, there should be a default context, which will be used when the URL path does not match. The virtual path of the default context can be set to "", "automatically converts to" / "during internal implementation; and its physical path can directly use the Tomcat's own / webAot / root content, or use custom content. Java code:
Context ctxtroot = Tomcat.
CreateContext
("" ", host.
GetAppBase
(
) "/ Root"
);
CTXTROOT.
SetPrivileged
(
True
);
Host.
AddChild
(CTXTROOT
);
It is worth noting that the root is set as the privilege program, which is mainly due to the ClassLoader of the Context container. Specific details Interested friends can refer to Tomcat About ClassLoading documents
Java code:
Bootstrap
|
SYSTEM
|
Common
/ /
Catalina Shared
/ /
WebApp1 WebApp2 ...
And the user's own WebApp is actually not limited to the same directory, fully set, using the way to create a root program. Finally, you need to create a suitable Connector to accept HTTP / HTTPS request. It is recommended to bind the web service on the local LoopBack address, limit that can only be accessed locally.
Java code:
Try
{
Tomcat.
AddConnector
Tomcat.
CreateConnector
(
Inetaddress.
GetByName
("
127.
0.
0.
1"
),
8080,
False
)
);
}
Catch
(
UnknownHOSTEXCEPTION E
)
{
SYSTEM.
Err.
PRINTLN
("Bind Tomcat Server TO
127.
0.
0.
1:
8080 failed. "
);
e.
PrintStackTrace
(
);
Tomcat =
NULL;
}
Complete embedded Tomcat creation code examples are as follows:
Java code:
Private Embedded CreateTomcat
(
String path
)
{
Embedded Tomcat =
New Embedded
(
);
Tomcat.
SetCataLinaHome
(PATH
);
Engine Engine = Tomcat.
CreateEngine
(
);
ENGINE.
SetName
("ESPSERVER"
);
Host host = Tomcat.
Createhost
("Localhost", Tomcat.
GetCataLinaHome
(
"/ WebApps"
);
ENGINE.
AddChild
(Host)
);
ENGINE.
SetDefaulthost
Host.
GetName
(
)
);
Context ctxtroot = Tomcat.
CreateContext
("" ", host.
GetAppBase
(
) "/ Root"
);
CTXTROOT.
SetPrivileged
(
True
);
Host.
AddChild (CTXTROOT
);
String esppath = configManager.
GetProperty
("ESP_ROOTDIR"
);
IF
(ESPPATH ==
null ||!
New
File
(ESPPATH
).
exists
(
)
)
{
ESPPATH = Host.
GetAppBase
(
"/ ESP";
IF
(!
New
File
(ESPPATH
).
exists
(
)
)
{
SYSTEM.
Err.
PRINTLN
("You Should Set ESP_ROOTDIR IN ESP.
"
);
Return
NULL;
}
}
Context CTXTesp = Tomcat.
CreateContext
("/ ESP", ESPPATH
);
Host.
AddChild
(CTXTesp
);
Tomcat.
Addengine
(ENGINE
);
Tomcat.
SetDebug
Logger.
Warning
);
Try
{
Tomcat.
AddConnector
Tomcat.
CreateConnector
(
Inetaddress.
GetByName
("
127.
0.
0.
1"
),
8080,
False
)
);
}
Catch
(
UnknownHOSTEXCEPTION E
)
{
SYSTEM.
Err.
PRINTLN
("Bind Tomcat Server TO
127.
0.
0.
1:
8080 failed. "
);
e.
PrintStackTrace
(
);
Tomcat =
NULL;
}
Return Tomcat;
}
Then you can call its START / STOP method to start or stop service at the time, it feels much more than the Tomcat configured in the standard. In addition, it can be used to obtain the current Tomcat version information for explicit states for explicit status. Detail issues in this process can be obtained by a secondary class. 1. When running this program, you need to use JDK instead of JRE, because Tomcat needs to dynamically compile the JSP page, you may also need to handle JDK /LIB/Tools.jar to the project classpath. 2. Because the embedded version Tomcat does not have a common / lib directory, if the bug found in the JAXP's Provider, you may need to copy XerceptImpl.jar and other implementations to the JDK / jRE / lib / endorsed directory. 3. Note Do not have other version Tomcat packages in ClassPath, otherwise conflicts may occur. If you need to learn more about information, you can refer to Tomcat to bring your Javadoc documentation, or O'Reilly's Embedding Tomcat Into Java Applications.