original:
Http://www.blogcn.com/user8/flier_lu/blog/4740621.html
When developing Tomcat-based web programs, a comparative headache is how to communicate with the front page from the front page. Although it is possible to support IDE-centric operational environment, it is convenient to configure trouble and a lot of speed and limit, 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 not changing the behavior, it can customize the debug environment yourself. 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, you can start Tomcat in the Java program in the ClassPath of the Java project after extracting the Tomcat Embed version. Similar to ordinary Tomcat configuration, it is running requires the structure of the following organization:
The following is program code:
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.
The following is the program code: Embedded tomcat = new Embedded (); tomcat.SetCatalinaHome (path); tomcat.addebug (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.
The following is program code: Engine Engine = tomcat.createEngine (); engine.SetName ("ESPSERVER" [IMG] /images/wink.gif [/ img]; host host = tomcat.createhost ("localhost", Tomcat.getCataLinaHome () "/Webapps"[img]/images/wink.gif[/img]; 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 convert to "/" during internal implementation; and its physical path can directly use the Tomcat's own / WebAPS / root content, or use custom content. The following is the program code: context ctxtroot = tomcat.createcontext ("", host.getappbase () "/root"[img]/images/wink.gif[/img]; ctxtroot.SetPriviled (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. Friends that are interested in specific details can refer to Tomcat
About ClassLoading documents
The following is program 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 to limit only local access.
The following is program code: try {tomcat.addconnector (inetaddress.getbyname ("127.0.0.1" [IMG] /IMAGES/Wink.gif [/ IMG], 8080, false);} catch (unknownhostException E ) {System.err.println ("Bind Tomcat Server to 127.0.0.1:8080 failed." [IMG] /images/wink.gif [/ img]; E.PrintStackTrace (); tomcat = null;}
Complete embedded Tomcat creation code examples are as follows:
The following program code is: private Embedded createTomcat (String path) {Embedded tomcat = new Embedded (); tomcat.setCatalinaHome (path); Engine engine = tomcat.createEngine (); engine.setName ( "EspServer" [img] / images /Wink.gif[/img]; host host = tomcat.createhost ("localhost", Tomcat.getcatalinaHome () "/webapps"[img]/images/wink.gif[/img]; engine.addchild (Host) Engine.SetDefaultHost (host.getname ()); context ctxtroot = Tomcat.createContext ("", host.getappbase () "/root"[img]/images/wink.gif[/img]; ctxtroot.setprivileged Host.addchild (ctxtroot); string esppath = configManager.getProperty ("ESP_ROOTDIR" [IMG] /images/wink.gif [/ img]; 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.CONFIG." [IMG. " ] /images/wink.gif [/ img]; return null;}} CONTEXT CTXTESP = Tomcat.createContext ("/ ESP", ESPPATH; Host.Addchild (CTXTESP); Tomcat.Addengine (Engine) Tomcat.SetDebug (Logger.Warning); try {tomcat.addconnector (inetaddress.getbyname ("127.0.0.1" [IMG] /Images/wink.gif [/ img], 8080, false);} Catch (unknownhostexception e) {system.err.println ("Bind Tomcat Server to 127.0.0.1:8080 failed." [img] /images/wink.gif [/ img]; E.PrintStackTrace (); tomcat = null;} Return Tomcat;} Then you can call its START / STOP method to start or stop service at the time, and it feels much more than the TOMCAT of the standard.
In addition, you can get the current Tomcat version information for explicit status by a secondary method serverInfo.getServerInfo ().
There are some details of the need to pay attention to during this process.
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
Embedding Tomcat Into Java Applications.