World class data recovery software and renowned raid recovery services

WestNIC provides reliable web hosting services

Ahosting.biz reseller hosting, managed dedicated server with 24/7 support

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.

Subscribe to this FAQ: RSS news feed

FAQ search

Understanding Tomcat

Q: What's the difference between Tomcat and a servlet container?

A: There is no difference between Apache Tomcat and a servlet container. Tomcat is the official reference implementation of the Java servlet and Java Server Pages specifications.

Q: Can I use Tomcat for a commercial application?

A: Yes, you can use Apache Tomcat for a commercial Web application and many companies do so.

Getting started with Tomcat

Q: How do I run a servlet in Tomcat?

A: Your servlet class files should be deployed to the classes subdirectory of WEB-INF under the relevant webapps directory, or packaged as a JAR in the lib subdirectory.

Premium Content: Follow this link for subscription information More details available to subscribers:
How do I run a servlet in Tomcat?

Q: What is the Tomcat-Apache service for?

A: This service configuration is given as an example of the WARP connector in the default Tomcat server installation and is not required for basic development purposes. The WARP connector allows Tomcat to establish two-way communication with a standard Apache HTTP server so they can delegate the response to Web requests. This means that Apache HTTPd can respond to requests for static content such as images, while Tomcat can service requests for JSP and servlets for example.

Unless you specifically want to make use of this feature, you can delete or comment-out this entire <Service> element from the configuration file.

Q: Should the Tomcat bootstrap classes be in my classpath?

A: Your compilation classpath only needs to include the classes and libraries necessary for your servlet. It's unlikely you are using Tomcat bootstrap classes in your servlet but, if so, make sure this package is on your classpath.

Premium Content: Follow this link for subscription information More details available to subscribers:
Should the Tomcat bootstrap classes be in my classpath?

Q: How can I get Tomcat to read JSP from a new test directory?

A: See this Code Style article about configuring new Web application contexts under the heading Tomcat server configuration: server.xml. Tomcat creates a "test" directory by default, though, so it would be best to name your new directory differently.

Q: What URL do I use to run my servlet?

A: The URL you use partly depends on the port you have configured Tomcat to operate on. This is configured in the HTTP Connector element in server.xml.

Premium Content: Follow this link for subscription information More details available to subscribers:
What URL do I use to run my servlet?

Q: Can I return custom error pages in Tomcat?

A: You can configure custom error pages in Tomcat using the error-page element in the application's web.xml file, as below. The error-code element contains the HTTP status code number, the location element contains the path to the error document relative to the Web application root. The error document may be a static HTML document or a JSP. The error-page element may be repeated to specify custom pages for any number of error codes.

Premium Content: Follow this link for subscription information More details available to subscribers:
Can I return custom error pages in Tomcat?

Tomcat diagnostics

Q: Tomcat 5 installation fails to find the JDK in my path!

A: Tomcat 5 and above uses the Java Runtime Environment by default, not the Java Software Development Kit (JSDK). The Tomcat installer attempts to use the Windows system registry to find a valid JRE installation or a JAVA_HOME environment variable, not the system path. If your JRE is not found or its registry entry is not valid it may cause problems during installation.

Check and upgrade your JRE installation to Java 1.5 if necessary, and try setting the JAVA_HOME variable to the path of the JRE base directory if still not found.

Q: The Tomcat window opens then closes immediately, what can I do?

A: To get more diagnostic information on this problem, open a command window and run the startup script manually: click Start select Run, type cmd and press Enter.

Premium Content: Follow this link for subscription information More details available to subscribers:
The Tomcat window opens then closes immediately, what can I do?

Q: Tomcat 5.5 on Windows just reports "terminated unexpectedly"!

A: This is not a very helpful error message. If you can run Tomcat in console mode, you may get more helpful information from the container output. Create a Windows shortcut with the following Target ...

Premium Content: Follow this link for subscription information More details available to subscribers:
Tomcat 5.5 on Windows just reports "terminated unexpectedly"!

Q: How can I stop out of environment errors with catalina.bat?

A: If you have many environment properties already set for other applications, Java packages and system devices, the addition of the Tomcat startup variables may exceed your initial allocation. Professional Apache Tomcat by Vivek Chopra et al. suggests you set environment space to 4096 bytes. If it still doesn't work at that level, you could try setting it extremely high to see if this makes the difference, then reduce it by trial and error to a working value.

Q: I get "specified service doesn't exist" with JDK 1.4!

A: Tomcat 5 is designed to run with the Java 1.5 runtime environment and does not work with the Java Software Development Kit (JSDK) version 1.4 without modification. So you have two options, either install the Java 1.5 runtime environment, or use the Apache Tomcat JDK 1.4 Compatibility Package, which includes additional JAR files for the Java Management Extensions API (JMX) and Xerces XML Parser.

Q: Tomcat reports "Unable to locate servlet.jar"!

A: Tomcat requires a system environment variable called TOMCAT_HOME to be set in advance, so that it knows where to find the key Java code it needs to run, including servlet.jar. This is normally found in the main installation directory for Tomcat. You also need to set a similar JAVA_HOME variable.

Premium Content: Follow this link for subscription information More details available to subscribers:
Tomcat reports "Unable to locate servlet.jar"!

Q: How can I log messages to the console?

A: The normal way of logging servlet information is to use the servlet's log(String) or log(String, Throwable) methods. To log a message when the servlet starts up, place the calls in the servlet's init(ServletConfig) method, as follows.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I log messages to the console?

Help request

Use the form below to submit a help request or general enquiry about the Code Style Web site. Before you write read the guidelines on asking the right questions, and check this page for periodic updates.

Information: Your email address will not be mis-used. If you include your address you may be sent a personal reply, you will not be added to any mailing list unless you request it. Read the site privacy statement for details.

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log