JAKARTA-Tomcat Confident Chinese User Guide Part 1
This document provides basic information about Tomcat. The main contents are as follows: Tomcat binary version installation with the main content of Tomcat related scripts related to server.xml, Tomcat's main configuration file How to set up Tomcat and host web server How to apply Tomcat Configuring a Realistic Web site I hope this document is enough to start using Tomcat. If you can't find someone, please find the Tomcat FAQ Find Tomcat Pack Refine Tomcat User Mail List Such questions do not exist, we encourage the answer to questions into Tomcat FAQ or this document. If you have any comments or suggestions for this document, please send it to Tomcat's mailing list.
Getting StartedTomcat is a servlet container with a JSP environment. Servlet container is a shell that manages and activates servlet based on user behavior. Roughly can divide the servlet container into a few categories:
The stand-alone servlet container has part of the web server. Refers to the use of Java-based web servers, such as servlet containers are part of JavaWebServer. Separate servlet container is the default mode of Tomcat. Most web servers are not based on Java, Therefore, we can draw the models of the following two containers.
The Servlet container servlet container in the process is a web server's plugin and the implementation of the Java container .Web server plug-in opens a JVM (Java Virtual Machine) to make Java containers internally. If there is a request to call servlet , The plug-in will acquire the control of this request and deliver him (using JNI) to the Java container. Processor Content For multi-thread, the server is very suitable and provides a good running speed, but the scalability is lacking.
The Servlet container servlet container runs outside the address space other than the web server and the combination of the web server's plug-in and the implementation of the Java container .Web server plugin, and Java container JVM use IPC mechanisms (usually TCP / IP) to communicate. When a request for calling a servlet arrives, the plugin will acquire the control of this request and deliver it (using IPC, etc.) to the Java container, the process outer container is not as good as the process instrument, but the process outer container The engine is better (scalability, stability, etc.) in many other comparable ranges. Tomcat can be used as a separate container (mainly for development and commissioning) as an additional server (current support Apache, IIS) And Netscape servers. You must decide how to apply him, such as choosing a second or third mode. What is the difference between Tomcat and JSERV? Tomcat is jserv? It is a common misunderstanding. Jserv is compatible with the container used with Apache .tomcat is a completely rewritten and container compatible with servlet API2.2 and JSP1.1.tomcat uses some JSERV. Code, especially the Apache interface of JSERV, but this is the only thing. How to install Tomcat's binary version? Very simple, just: Download any compressed file from http://jakarta.apche. ORG / DOWNLOAD / BININDEX.HTML. Unzip this file to a directory (such as: foo). A subdirectory will be generated, named "Tomcat". Convert to "Tomcat" directory Setting up a new environment variable (Tomcat_home) Point to the Tomcat of Tomcat, type: "set tomcat_home = foo / tomcat" UNIX platform: If it is a Bash / SH environment, type: "Tomcat_home = foo / tomcat;
Export Tomcat_Home "If the TCSH environment, type:" setenv tomcat_home foo / tomcat "Set the environment variable java_home points to your JDK directory, then add the Java interpreter to your PATH environment variable. Ok! Now you can run Tomcat and as an independent Servlet Container (Mode 1) Starts with Close Tomcat Using Scripts in the "bin" directory launch and close Tomcat. Start: uinx: bin / startup.shwin32: bin / startup close: UNIX: bin / shutdown.shwin32: bin / shutdowntomcat directory Structure Hypothesis You have decompressed Tomcat, you have got the following directory structure: Directory Name - Description BIN contains Start / Close script Conf contains different profiles, including server.xml (Tomcat's primary configuration file) and for different Tomcat configurations Web Application Settings Defaults File Web.xmldoc Contains Various Tomcat Document Lib Contains Tomcat JAR Files. UNIX Platform Any files in this directory are added to Tomcat's ClassPath Logs Tomcat placed in Logs Tomcat placed in Logs Tomcat ServletAPI source files. Don't happy, these only some of the air interfaces and abstract class WebAPPs that must be implemented in the servlet container contains web project examples. Some you can create the following directory: Worktomcat automatically generates, place Tomcat Timatics (such as Compiled JSP files). If you delete this directory when you run. Thejsp page will not be able to run. Classes You can create this directory to add some additional categories to classpaths. Anything you add this directory can be Finding itself in Tomcat's class path Tomcat is a Java program, so you can run how to use the command line after setting several environment variables. However, set each environment variable used by Tomcat and the following command line parameters. And error. Therefore, Tomcat development group provides some scripts to make the startup and closing Tomcat easier. Note: These scripts are just a convenient way to start and close Tomcat. You can modify them to customize ClassPath, environment variables such as Path, LD_LIBRARY_PATH,, etc., as long as it produces a correct command line. What is these scripts? The following table lists the most important scripts for the general user. Tomcat main script. Set the appropriate environment variable, including ClassPath, Tomcat_Home and JAV A_HOME and start TomcatStartup with a suitable command line parameter
Launch Tomcat in the background. Replacement method of "Tomcat Start" command
Shutdown
Close Tomcat. "Tomcat Stop" replacement method
The most important script for the user is Tomcat (Tomcat.bat / Tomcat.sh). Other Tomcat-related scripts as a simple-oriented entry to Tomcat scripts (setting different command line parameters, etc.).
Carefully examine tomcat.bat / tomcat.sh, follow these steps:
Steps under UNIX:
Specify Tomcat_Home if not specified
If not specified, speculate on java_home
Set the class_path contains:
1. $ {Tomcat_home} / classes directory (if present)
2. $ {Tomcat_home} / lib's content
3. $ {java_home} /lib/tools.jar (This JAR file contains tool javac, we need JAVAC to handle JSP files.
Run the java command with command line parameters with setting Java environment variables, tall tomcat.home, and org.apache.tomcat.startup.tomcat as start-up class. At the same time, the command line parameters are also passed to org.apache.tomcat. Startup.tomcat, for example:
Execute the operation of start / stop / run
This Tomcat process uses the path to Server.xml,
For example, Server.xml is placed in ETC / Server_1.xml and the user is intended to start Apache in the background, the key is as follows:
BIN / TOMCAT.SH START? F /ETC/Server_1.xml
Steps under Win32: ()
It can be seen that the Win32 version of Tomcat.Bat is almost consistent with UNIX version. Especially in it does not speculate Tomcat_Home and Java_Home value, and do not place all JAR files into the class path. Tomcat configuration file
Tomcat configuration is based on two profiles:
1.Server.xml - Tomcat global profile
2.Web.xml - Configuring different relationship environments in Tomcat
This section will tell how to use these files. We will not contain the internal mechanism of web.xml, these internal mechanisms go deep into the details of the Servlet API, so we will discuss the contents of servler.xml and web.xml in the Tomcat relationship environment usage.
Server.xml
Server.xml is the primary configuration file of Tomcat. Complete two goals:
1 Provides an initial configuration of Tomcat components.
2 illustrate the structure of Tomcat, so that Tomcat is started through instantiation components and build itself, as specified in Server.xml.
The following table describes the important elements of the Server.xml:
Element and description
The most important element in the ServerServer.xml file. Server defines a Tomcat server. Generally, you don't have to worry too much. Server element can contain Logger and ContextManager element types.
Logger This element defines a logger object, each logger has a name to logo, and there is also a record Logger output and redundancy level (describe this log level) and a path to log files. Usually there is a servlet's logger (servletContext.log () The Logger at the time of JSP and Tomcat.
ContextManagerContextManager Description A set of ContextInterceptor, RequestInterceptor, Context, and their Connectors configuration and structure. ContextManager has several features available: 1. Debug debugging levels 2. WebApps /, conf /, logs / and all have The basic location of the defined environment. It is used to start Tomcat to start outside of Tomcat_home. 3. Name of the work directory
Contextinterceptor & RequestIntercePtor These listeners listened to events that occur in ContextManager. For example, ContextInterceptor listens to Tomcat's launch and termination event, Requestinterceptor monitors different stages of the user requesting to pass during its service process. Tomcat does not have to Know that there are too many knowledge about listeners; in addition, developers should know how this is how to implement a "global" operation in Tomcat (such as security and each request log)
ConnectorConnector represents a user's join, whether it is through the web server or directly to the user browser (in a stand-alone configuration) .Connector is responsible for managing Tomcat's working thread and read / write to different users' ports. Convenector The configuration contains the following information:
Handle class
2. TCP / IP port of the handle listener
3. Backlog of the TCP / IP of the handle server port.
We will describe how to configure Connector in this document later.
Context Each Context provides a subordinate directory that pointing you to Tomcat of your web project. Each context contains the following configuration:
1. The path placed by the Context can be the path associated with the ContextManager home directory.
2. Record debugging level of debugging information
3. Overloaded flag. When developing servlets, overloading the changes after the changes, this is a very convenient feature, you can debug or use Tomcat to test the new code without stopping or restart Tomcat. To open overload, Reloadable is set to true. This takes time but detects changes to changes; more important things, in view of the load trigger, the class load trigger may throw some errors when a load-bearing object is loaded with a new servlet. To avoid these issues, you can set overloaded as a false, which will stop overload functionality.
Start Tomcat from another directory
As the default value, Tomcat_Home / conf / server.xml will be used as a configuration file. The default configuration will use tomct_home as the basis for the relational environment. Use the "-f / you / Directory / Server.xml" option you can change this situation You need to set the following files in the primary directory using another server profile and setting relational environmental manager you need to set the following files in the home directory:
? A webApps / directory (if you have generated)? All WAR file trophy is overwhelming this directory and all subdirectories are added as a relational environment.
? conf / directory - you can save a special web.xml file and other profiles
• logs / - All log files will replace Tomcat_home / logs / record to this directory
? Work / - work catalog of relational environments
If the ContextManager.home attribute in Server.xml is associated, it will be associated with the current working directory.
Web. xml
About the web. A detailed description of the XML and Web project structures (including directory services and configurations) can be found in Chapters 9, 10, 14 of Servlet API SPEC.
However, there is a small "characteristic" related to Tomcat. Tomcat allows users to define the default value of all the Web.xml of all relationship environments by putting the default web.xml into the confed directory. When establishing a new relationship environment, Tomcat uses the default web.xml file as the basics. Set and apply item-specific web.xml (web-inf / web.xml files for application items) to override these defaults.
Set Tomcat and Apache Web Server
server
So far, we did not discuss Tomcat-extended Tomcat, only discussed servers running independently. But there are some problems need to be explained:
1. When the static page is processed, Tomcat is not as fast as Apache.
2. Tomcat is not as configurable as Apache.
3. Tomcat is not as strong as Apache.
4. There are many websites that have been put on a particular web server for a long time, for example, using CGI scripts / Server API modules / perl / php ... We can't assume that these remainings will be discarded.
Based on the above reasons, a real-world website recommends using a web server, such as Apache, serving a service for the static page of the website, and use Tomcat as a servlet / jsp plugin.
We are not prepared to discuss each different configuration, we will:
1. Cover the basic behavior of the web server
2. Explain what configuration needs
3. Examples on Apache
WEB server operation
Simply put, the web server is always waiting for HTTP requests from the client. When the request arrives, the server will provide everything necessary to meet this request. Add a servlet container to some extent change this behavior. But the server still needs to handle the following factors:
Load the servlet container interface library and initialize (before processing the request).
When a request is received, check if it belongs to a servlet, if so, the interface library receives this request and processes.
On the other hand, the interface library needs to know that he will serve some request, usually a mode based on the request, and where to guide this request.
When the user wants to set the configuration using the virtual host, things become more complicated, or want multiple developers to develop JVMs for developing but use different servlet containers. We will discuss these two issues.
Required configuration
The most obvious configuration should be considered is that the servlet URL has a reputation of servlets in the servlet container. Obviously, you must know what to pass on the servlet container. We still need to provide additional configuration items into the coincidence of Web-Server or Servlet-Container.
l Whether the Tomcat process can be obtained, we still need to provide the TCP / IP hostname / port number of configuration and Tomcat is listening.
l Need to inform the web server interface library (so we can put it in the start of)
l Need to set the interface internal information such as where to record the log, and how to record, and so on.
All such information must appear in a Web server configuration or in a private configuration file used by the interface. Here's how to implement these configurations in Apache. Implementation on Apache
This section demonstrates how to configure Apache to work with Tomcat; and try to explain the configuration specification that may be used in depth. Other information can be found on the JSERV installation page.