1. Understand tomcat
Tomcat is not a complete Java EE (j2ee) server because it does not provide a complete API of the Java EE enterprise application platform. However, since Tomcat follows the apache open source protocol and perfectly supports the open source components of the current Java development framework, Structs, Spring and Hibernate, tomcat is used by many enterprises to deploy and configure many Java applications to replace some commercial Java application servers.
2. Tomcat's directory structure
To deploy and use tomcat, you must understand the directory structure of tomcat and the role of each directory. Take tomcat7 as an example here.
Enter the tomcat installation directory:
|-- bin
| |-- bootstrap.jar A class that tomcat depends on when it starts up. When you start tomcat, you will find Using CLASSPATH: is the loaded class
| |-- catalina-tasks.xml defines tomcat Loaded library files, class files
| |-- catalina.bat
| |-- catalina.sh tomcat single instance startup script on Linux platform
| |-- commons-daemon-native.tar.gz jsvc tool, can make Tomcat is already running as a daemon process and needs to be compiled and installed separately
| |-- commons-daemon.jar Java classes that the jsvc tool depends on
| |-- configtest.bat
| |-- configtest.sh tomcat is a Linux platform script that checks the configuration file syntax is correct
| |-- cpappend.bat
| |-- daemon.shtomcat is running in daemon mode, start, stop script
| |-- digest. bat
| |-- digest.sh
| |-- setclasspath.bat
| |-- setclasspath.sh
| |-- shutdown.bat
| |-- shutdown.shtomcat service shutdown script under Linux platform
| |-- startup.bat
| |-- startup.sh tomcat service startup script under Linux platform
| |-- tomcat-juli.jar
| |-- tomcat-native.tar.gz Enable tomcat to use apache's apr runtime library to enhance the performance of tomcat Need to compile and install separately
| |-- tool-wrapper.bat
| |-- tool-wrapper.sh
| |-- version.bat
| `-- version.sh View tomcat and JVM version information
|-- conf As the name suggests, configure File Directory
| |-- catalina.policy configures tomcat's read, write and execute permissions on directories or files in the file system, and management permissions for some memory, session, etc.
| |-- catalina.properties configures tomcat's classpath, etc.
| |-- context.xml The default context container
of tomcat | |-- logging.properties configures the log output mode of
tomcat | |-- server.xml tomcat's main configuration file
| |-- tomcat-users.xml tomcat's role (authorized user) configuration file
| `-- web.xmltomcat application deployment descriptor file
|-- lib
|-- logs log file default storage directory
|-- temp
| `-- safeToDelete.tmp
|-- webapps tomcat default storage application directory , just like apache's default web page storage path is /var/www/html
| |-- docstomcat documentation
| |-- examples An example of an independent web application that comes with tomcat
| |-- host-manager tomcat's host management application Program
| | |-- META-INF The entry of the entire application, used to describe the information of the jar file
| | | `-- context.xml The context container configuration of the current application, it will override the configuration in tomcat/conf/context.xml
| | |-- WEB-INF is used to store the private resources of the current application
| | | |-- classes is used to store the class files required by the current application
| | | |-- lib is used to store the jar files required by the current application lock
| | | `-- web.xml The deployment descriptor file of the current application , define the serverlet class to be loaded by the application, and how the program is deployed
| |-- manager tomcat's management application
| |-- ROOT refers to the root of the tomcat application, if the application is deployed in ROOT, you can Direct access to
`--work through http://ip:port is used to store the class file generated after the JSP application is compiled during deployment
3. Understand the structure and meaning of the main configuration file (server.xml) of tomcat
as shown in the figure below , the front-end request is received directly by tomcat or by the front-end proxy, through HTTP, or AJP proxy to Tomcat. At this time, the request is received by the connector in tomcat. Different connectors and engines are associated with the service component, and many engines are defined in one engine. The virtual host is defined by the Host container. Each Host container represents a host. In the respective Host, multiple Contexts can be defined, which are used to define multiple independent applications in a virtual host. 4. Configure an example of Java code
for a single instance application
Planning:
Website Web Directory: /web/www Domain Name: www.test1.com
Forum Web Directory: /web/bbs URL: bbs.test1.com/bbs
Web Manager: $CATALINA_HOME/wabapps URL: manager.test.com Allowed Access address: 172.23.136.*
conf/server.xml
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
< Listener className="org.apache.catalina.core. ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<!-- Globally named resources to define some external access resources, whose role is the definition of external resources referenced by all engine applications--!>
<Resource name="UserDatabase" auth="Container"
type="org .apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
< !-- Define an authentication resource named "UserDatabase", load conf/tomcat-users.xml into memory, and authenticate in memory when authentication is required-->
<Service name="Catalina">
< !-- # Define the Service component, and associate Connector and Engine together. One Engine can correspond to multiple Connectors, and each Service can only have one Engine --!>
< Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<!-- Modify the connector listening port of HTTP/1.1 to 80. The request that the client accesses through the browser can only be passed to tomcat through HTTP. -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="test.com">
<!-- Modify the current Engine, the default host is , www.test.com -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm >
# Realm component, defines the authentication for application access in the current container, and authenticates through the external resource UserDatabase
<Host name="test.com" appBase="/web" unpackWARs="true" autoDeploy="true">
<!-- Define an alias www.test.com, similar to apache's ServerAlias -->
<Context path="" docBase="www/" reloadable="true" />
<!-- Define the application, access path "", that is, you can visit www.test.com. The web page directory is: relative to www/ under appBase, that is, /web/www, and when there are relevant changes in web.xml or classes under the application, it will be automatically Reload the current configuration, that is, without restarting tomcat to make the deployed new application take effect -->
<Context path="/bbs" docBase="/web/bbs" reloadable="true" />
<!-- Define another independent application, the access path is: www.test.com/bbs, the application web page directory is /web/bbs -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/web/ www/logs"
prefix="www_access." suffix=".log"
pattern="%h %l %u %t "%r& quot; %s %b" />
<!-- Define a Valve component to record the access log of tomcat. The log storage directory is: /web/www/logs If it is defined as a relative path, it is equivalent to $CATALINA_HOME, not relative to appBase, so pay attention to this. Define the log file prefix as www_access. and end with .log, pattern defines the log content format, and the specific fields indicate that you can view the official tomcat documentation -->
</Host>
<Host name="manager.test.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- Define a host name as man.test.com, the application directory is $CATALINA_HOME/webapps, automatic decompression, automatic deployment -->
<Valve className="org. apache.catalina.valves.RemoteAddrValve" allow="172.23.136.*" />
<!-- Define the remote address access policy, only allow the 172.23.136.* network segment to access the host, others will be denied access-- >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/web/bbs/logs"
pattern="%h %l %u %t "%r" %s %b" />
<!-- define the access log for this host -->
</Host>
</Engine>
</Service>
</ Server>
conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui" />
<!-- define a role Named: manager-gui -->
<user username="cz" password="manager$!!110" roles="manager-gui" />
<!-- Define a user's username and password and assign it to manager -gui roles -->
</tomcat-users>