Cover | Table of Contents | Colophon
init script that came with that package to start and stop Tomcat. Or, if you installed Tomcat on Windows via the graphical installer from tomcat.apache.org, you should start and stop Tomcat as you would any Windows service. Details about each of these package-specific cases are given in the next several sections. If you installed Tomcat by downloading the binary release archive (.zip or .tar.gz) from the Tomcat downloads page—what we'll call the generic installation case—you should use the command-line scripts that reside in the CATALINA_HOME/bin directory.Script | Purpose |
|---|---|
catalina | The main Tomcat script. This runs the java command to invoke the Tomcat startup and shutdown classes. |
cpappend | This is used internally, and then only on Windows systems, to append items to Tomcat classpath environment variables. |
digest | This makes a crypto digest of Tomcat passwords. Use it to generate encrypted passwords. |
service | This script installs and uninstalls Tomcat as a Windows service. |
init scripts, and Windows users will need to set Tomcat up as a service. Both approaches are outlined in this section.chkconfig, as the root user you can simply chkconfig tomcat on for the run level(s) of your choice.chkconfig command to make the tomcat service start in the run level(s) of your choice. Here's an example of how to make it start in run levels 2, 3, 4, and 5:
# chkconfig --level 2345 tomcat on
chkconfig does not see the tomcat service, try tomcat55 instead (the JPackage.org RPM package's init script has this name). Otherwise, you probably did not install Tomcat as an RPM package. Below, we show how to add a simple init script to make it work anyway.# chkconfig --list tomcat tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off
init script, but it is simple to create one that would just start Tomcat at boot time and stop it on shutdown.- is a very simple Tomcat init script for Linux.#!/bin/sh # Tomcat init script for Linux. # # chkconfig: 2345 96 14 # description: The Apache Tomcat servlet/JSP container. JAVA_HOME=/usr/java/jdk1.6.0_02 CATALINA_HOME=/opt/apache-tomcat-6.0.14 export JAVA_HOME CATALINA_HOME exec $CATALINA_HOME/bin/catalina.sh $*
Connector element in the server.xml file. Find the XML tag that looks something like this:
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
root account on Linux, Solaris, BSD, and other non-Windows operating systems.root, but we have not heard even a single reported incident where a machine's security was compromised because Tomcat was running as root. If you're worried about this, there are other ways of making Tomcat answer on port 80 without running Tomcat's JVM process as root. The following sections explain a few ways of doing just that.root user in order to open a server socket on port 80 on non-Windows operating systems. But, the JVM would not need to run as root if something outside the JVM process could relay all port 80 TCP connections to Tomcat on some port higher than 1024 (such as port 8080, for example). Tomcat can open its web server on port 8080, and something else with the proper permissions can relay port 80 TCP connections to Tomcat's port 8080. This is often referred to as Use | JVM option | Meaning |
|---|---|---|
Memory setting | -Xms384M | Sets the heap memory size at JVM startup time. |
Memory setting | -Xmx384M | Sets the maximum heap memory size the JVM can expand to. |
Debugging security | -Djava.security.debug=all | Turns on all debug output for security. |
Debugging | -enableassertions | Enables assertion checking. |
Debugging | -verbose:class | Enables verbose class loading debug output. |
Debugging | -verbose:gc | Enables verbose garbage collection debug output. |
Graphical | -Djava.awt.headless=true | Allows the JVM to run without any graphical display software installed. |
Localization | -Duser.language=en | Sets the language bundle that Tomcat uses. |
Localization | -Dfile.encoding=UTF-8 | Sets the default file encoding that Tomcat uses. |
Networking | -Djava.net.preferIPv4Stack=true | Configures the JVM to use IPv4 instead of IPv6; thus, any misconfiguration of IPv6 does not prevent Tomcat from working properly over Ipv4. On some operating systems such as FreeBSD, this switch appears to be required for Tomcat to work. |
javac command. It is a relatively new Java compiler, and as such may not be as mature or as robust as javac, or other older compilers.javac compiler, since Tomcat contains its own Java compiler, Tomcat does not need the JDK's Java compiler, as long as Tomcat's bundled Java compiler can compile everything that javac can. Because the JDT compiler is newer, it is still maturing, and you may find that some Java 1.5 or 1.6 source code language features are not fully implemented yet. Because it is the Java compiler that the Eclipse IDE uses, quite a bit of effort is going into making it both complete and robust, and there are a large number of people using it and testing it. Still, you may run into a situation where you want to switch Tomcat between the JDT compiler and your JDK's javac compiler.javac compiler.<security-constraint> in a webapp's WEB-INF/web.xml file define how user and role information will be stored and how users will be authenticated for the webapp. There are many ways of configuring each; feel free to mix and match.UserDatabaseRealm, JDBCRealm, JNDIRealm, and JAASRealm. Java developers can create additional realm implementations to interface with their own user and password stores as well. To specify which realm should be used, insert a HttpSession object that temporarily stores information about a user, including a unique session identifier and references to Java objects that the web application stores as attributes of the session. Typical uses of sessions include shopping carts and sites that require users to sign in. Usually, sessions are set to time out after a configurable period of user inactivity, where user inactivity is defined as a pause in requests belonging to the HTTP session. Once a session has timed out, it is said to be an invalid session, and if the user makes a new HTTP request to the site a new, valid session has to be created, usually through a re-login.Managers that handle the logic about how sessions are handled and session Stores to save and load sessions. Not every Manager uses a Store to persist sessions; it is an implementation option to use the Store interface in order to provide pluggable session store capabilities. Robust session Managers will implement some kind of persistent storage for their sessions, regardless of whether they use the Store interface. Specifying a Manager implementation works in a similar fashion to specifying a Realm:
<Manager className="some.manager.implementation.className"
customAttribute1="some custom value"
customAttribute2="some other custom value"/>
Manager and Store objects, but some options are set in web.xml, that is, at the context level. These options are described in detail in .Manager is an HTTP session manager. Do not confuse it with the Manager web application described in .
<resource-ref>
<description>
The database DataSource for the Acme web application.
</description>
<res-ref-name>jdbc/JabaDotDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Context ctx = new InitialContext( );
DataSource ds = (DataSource)
ctx.lookup("java:comp/env/jdbc/JabaDotDB");
Connection conn = ds.getConnection( );
... Java code that accesses the database ...
conn.close( );
DataSource is an object that can hand out JDBC Connection objects on demand, usually from a pool of preallocated connections.Context container element. Find the Context element for your webapp, and insert a Resource element similar to the one shown in .
<!-- Configure a JDBC DataSource for the user database. -->
<Resource name="jdbc/JabaDotDB"
type="javax.sql.DataSource"
auth="Container"
user="ian"
password="top_secret_stuff"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql:jabadot"
maxActive="8"
maxIdle="4"/>
reloadable attribute in the web application's Context element (in either your server.xml or your context XML fragment file, wherever you've stored your Context element), and restart Tomcat. Once you've done this, you can still reload the servlet classes in a given Context by using the Manager application (detailed in the section "" in Chapter 3).http://www.cs.myuniversity.edu/˜ian http://members.mybigisp.com/˜ian
Listener elements. The Listener's className attribute should be org.apache.catalina.startup.UserConfig, and the userClass attribute specifies one of several mapping classes. If your system runs Unix and has a standard /etc/passwd file that is readable by the account running Tomcat, and that file specifies users' home directories, use the PasswdUserDatabase mapping class:<Listener className="org.apache.catalina.startup.UserConfig" directoryName="public_html" userClass="org.apache.catalina.startup.PasswdUserDatabase"/>
HomesUserDatabase class:<Listener className="org.apache.catalina.startup.UserConfig" directoryName="public_html" homeBase="/home" userClass="org.apache.catalina.startup.HomesUserDatabase"/>
# cd $CATALINA_HOME # mkdir moved-webapps # mv webapps/* moved-webapps/
#! /usr/bin/python # Trivial CGI demo print "content-type: text/html" print "" print "<html><head>Welcome</head>" print "<body><h1>Welcome to the output of a CGI under Tomcat</h1>" print "<p>The subject says all.</p>" print "</body></html>"
CATALINA_HOME directory of Tomcat.admin role. There is no "default user," for security reasons. In CATALINA_HOME/conf/tomcat-users.xml, add a role with the name "admin", and make sure your user account's role memberships include "Host. A host represents a fully qualified domain name or IP address, such as groovywigs.com, for example. The stock Tomcat server.xml configuration file has a default host named localhost. The fact that this Host is the default Host as well as the only Host means that all HTTP requests entering Tomcat will be mapped to this Host, regardless of what host name is specified in the HTTP requests. For example, if the Host header in an incoming HTTP request says groovywigs.com as the host that the request is destined for, it won't be a match for the only Host name that Tomcat knows about (localhost), so Tomcat will instead map it to the default Host: the same one named localhost.ROOT. You could deploy that as webapps/ROOT. In this case, the host's name is groovywigs.com.<Host> XML element is defined. Then, add a new <Host> element above it, like this:<Host name="groovywigs.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <!-- Context elements for the groovywigs.com host go here. --> </Host> <!-- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Engine>'s default host, also in server.xml:
<!-- Define the top level container in our container hierarchy -->
<Engine name="Catalina" defaultHost="
/ /index.jsp /products.jsp /widgets/index.html /widgets/pricing.jsp /images/logo.png /WEB-INF/web.xml /WEB-INF/classes/com/acme/PriceServlet.class /WEB-INF/classes/DataHelper.class /WEB-INF/lib/acme-util.jar