Tomcat - Java Container

Tomcat is both an HTTP server and a Servlet container

Installation

apt-get install default-jdk
wget http://www.us.apache.org/dist/tomcat/tomcat-8/v8.0.9/bin/apache-tomcat-8.0.9.tar.gz
tar zxvf apache-tomcat-8.0.9.tar.gz
cd apache-tomcat-8.0.9
bin/startup.sh

Structure

bin

Holds the scripts for starting and stopping tomcat

daemon.sh

setenv.sh

CATALINA_OPTS=”$CATALINA_OPTS -server -Xms600m -Xmx600m -Xmn200m -XX:userParNewGC -XX:ParallelGCThreads -XX:UseParallelGC -Djava.awt.headless=true -Xloggc:/tmp/gc.logs”

CATALINA_OPTS=”$CATALINA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=20002”

conf

Contains various configuration files

server.xml

tomcat’s main configuration file, containing components such as service, connectors, engine, realm, valve, hosts, etc.

<Server port="8005" shutdown="SHUTDOWN">

Server is the top-level component in Tomcat; it can contain multiple Service components. port specifies a port responsible for listening for requests to shut down tomcat; shutdown specifies the command string sent to that port.

<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />

Apr mode is supported by default

<Service name="Catalina">

The Service component is essentially a wrapper around Connector and Engine components; it associates one or more Connector components with an Engine. name specifies the service’s name

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" address="127.0.0.1" />

Connector [2] is the component in Tomcat that listens for TCP network connections; each Connector listens on a separate port to handle connections from clients. port specifies the port number the server should create and listen on for client requests. protocol is the startup protocol. maxThreads sets the maximum number of threads listening on the port, which also determines the maximum number of client requests the server can respond to concurrently (default 200). acceptCount specifies how many requests can be queued once all available request-handling threads are in use; requests beyond this count are not handled (default 100). minProcessors is the number of request-handling threads created at server startup. maxProcessors is the maximum number of request-handling threads that can be created. minSpareThreads is the minimum number of spare threads. maxSpareThreads is the maximum number of spare threads. connectionTimeout specifies the timeout duration (in milliseconds). redirectPort specifies the port to redirect to if the server, while handling an HTTP request, receives an SSL transport request. enableLookups: if true, the actual hostname of the remote client can be obtained via a DNS lookup by calling request.getRemoteHost(); if false, no DNS lookup is performed and the IP address is returned instead. address: if the server has more than one IP address, this attribute can set which IP address the port listens on — by default the port listens on all of the server’s IP addresses. debug is the log level. disableUploadTimeout disables the upload timeout, mainly used when uploading large amounts of data

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

AJP stands for Apache Jserv Protocol; this connector handles interaction between Tomcat and an Apache HTTP server. It’s used when combining Tomcat with an Apache HTTP server — for example, if an Apache HTTP server and multiple Tomcat servers are deployed on the same physical server, and Apache handles static resources and load balancing, each Tomcat instance needs AJP to listen on a different port

<Engine name="Catalina" defaultHost="localhost">

An Engine can contain one or more Hosts, meaning a single Tomcat instance can be configured with multiple virtual hosts. name defines the Engine container’s name, corresponding to Catalina in $CATALINA_HOME/config/Catalina. defaultHost specifies the default hostname for handling requests; it must match the name attribute of at least one of the host elements. debug is the log level

<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

name specifies the hostname; a virtual host can have multiple Contexts. appBase is webapps, i.e. the \webapps directory. The unpackWARs attribute specifies whether war packages in the appBase directory are automatically unpacked. autoDeploy specifies whether war packages added to the appBase directory are automatically deployed

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />

Valve means “valve” in English; Valve is Tomcat’s implementation of the chain-of-responsibility pattern, processing requests by chaining multiple Valves together. A Valve can be defined in any container — Engine, Host, and Context, mentioned above, are all containers. className specifies the class name the Valve uses. By default, a class named org.apache.catalina.valves.AccessLogValve is defined, responsible for intercepting every request and then logging the application’s access information. directory specifies where the log file is stored. pattern has two values: the common style logs the remote hostname or IP address, username, date, the first line of the request string, the HTTP response code, and the number of bytes sent; the combined style logs even more than common. It’s recommended to use pattern=”%{X-Real-IP}i %l %u %t "%r" %s %b %{User-Agent}i” to log the real IP.

<Context docBase="ROOT" path="" debug ="0" allowLinking="true" />

In Tomcat, every running webapp ultimately exists in the form of a Context, and every Context has a root path and a request URL path. In Tomcat we usually create a Context in one of two ways: creating a directory under the \webapps directory, which automatically creates a context whose default access URL is http://host:port/dirname; or adding a context element inside an element in server.xml. Either way, we can then access the configured context via http://host:port/mypath.

web.xml

A configuration file that follows the Servlet specification, used to configure servlets and provide default configuration information, including MIME mappings, for all web applications

<error-page>
  <error-code>404</error-code>
  <location>/404.html</location>
</error-page>
<error-page>
  <error-code>500</error-code>
  <location>/500.html</location>
</error-page>

catalina.properties

Defines Tomcat’s internal packages and access-related controls, including control over content loaded through class loaders; Tomcat reads the relevant settings from this file in advance at startup

Class Loader HOW-TO

Understanding Tomcat’s Classpath — Common Problems and How to Solve Them

lib

Holds the jar files tomcat needs

logs

Holds log files

temp

Temporary files

webapp

Holds example applications; deployed applications also go in this directory

work

Holds the class files produced after JSP compilation

Connector modes

tomcat has 3 connector modes

  • HTTP connector

Based on the HTTP protocol, responsible for establishing HTTP connections. It’s further split into the BIO http connector and the NIO http connector.

  1. BIO (blocking I/O) HTTP connector — the default mode; blocking I/O operations, very low performance, no optimization or special support applied.

  2. NIO (new I/O) HTTP connector is a buffer-based Java API that supports non-blocking I/O operations, which is why NIO is also seen as short for “non-blocking I/O” — it has better concurrent performance than traditional I/O (bio). Just change protocol in the connector node to org.apache.coyote.http11.Http11NioProtocol

  • AJP connector

Based on the AJP protocol; AJP is a protocol specifically designed for communication between tomcat and an http server, offering higher communication speed and efficiency. This protocol is used when integrating with an Apache server.

  • APR HTTP connector [3]

Solves asynchronous I/O problems at the system level, greatly improving performance. Supported by default, but requires installing the APR library | JNI wrappers for APR used by Tomcat (libtcnative) [4] | OpenSSL libraries

apt-get install libapr1-dev libssl-dev
tar zxvf tomcat/bin/tomcat-native.tar.gz
cd tomcat/bin/tomcat-native-1.1.*-src
./configure --with-apr=/usr/bin/apr-1-config \
           --with-java-home=/usr/bin/java \
           --with-ssl=yes \
           --prefix=/usr/
make && make install

libtcnative will be installed to /usr/lib

Tomcat connectors will automatically enable APR mode

References