Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Rephrase

...

This section provides several recommendations on how to make your web application and Tomcat as a whole to start up faster.

Before we continue to specific tips and tricks, the general advice is that if Tomcat hangs or is not responsive you have to perform diagnostics. That is to take several thread dumps to see what Tomcat is really doing. See Troubleshooting and Diagnostics page for details.

JAR scanning

The Servlet 3.0 specification (chapter 8) introduces support for several "plugability features". Those exist to simplify a structure of a web application and to simplify plugging of additional frameworks. Unfortunately, these features require scanning of JAR and class files, which may take noticeable time. Conformance to the specification requires that the scanning were performed by default, but you can configure your own web application in several ways to avoid it (see below). It is also possible to configure which JARs Tomcat should skip.

For further talk, the features that require scanning are:

Introduced by Servlet 3.0:

  • SCI (javax.servlet.ServletContainerInitializer (shortened as SCI)
  • Web fragments (META-INF/web-fragment.xml)
  • Using annotations to define components Resources of a web application (Servlets etc.)
  • Using annotations to define components processed by an SCI (@HandlesTypes annotation on a SCI)
  • Packing web application resources bundled in jar files (META-INF/resources/*)

...

  • Annotations that define components of a web application

...

  • (@WebServlet etc.)
  • Annotations that define components for 3-rd party libraries initialized by an SCI (arbitrary annotations that are defined in @HandlesTypes annotation on a SCI class)

Older features, from earlier versions specifications:

  • TLD scanning, (

These features require scanning the JAR files. The worst is scanning for annotated classes. There are a lot of class files to process and parsing a class file takes noticeable time.

It is possible to configure a web application to omit most of the scanning. It is also possible to configure which JARs Tomcat should skip.

Other features that require scanning are:

  • Discovery of tag libraries. That is scanning for Tag Library Descriptor files (, META-INF/**/*.tld) (shortened as TLD scanning)
    .

Among the scans the annotation scanning is the slowest. That is because each class file (except ones in ignored JARs) has to be read and parsed looking for annotations in itThe TLD scanning is done at startup, because a library can define a Listener in its TLD file.

A note on TLD scanning: in In Tomcat 7 and earlier the TLD scanning happens twice, (1) by servlet engine ,

  • first, at startup time, to discover

...

  • listeners

...

  • declared in tld files (done by TldConfig class),
  • second, by JSP engine when generating java code for a JSP page (

...

  • done by TldLocationsCache).

The second scanning is more noticeable, because it prints a diagnostic message about scanned JARs that contained no TLDs. In Tomcat 8 the TLD scanning happens only once at startup time (in JasperInitializer).

Remove unnecessary JARs

Remove any JAR files you do not need. When searching for classes every JAR file needs to be examined to find the needed class. If the jar file is not there - there is nothing to search.

Note that a web application should never have its own copy of Servlet API or Tomcat classes. All those are provided by the container (Tomcat) and should never be present in the web application. If you are using Apache Maven, such dependencies should be configured with <scope>provided</scope>. See also a stackoverflow page.

Configure your web application

See chapter in Tomcat 7 migration guide.

...

There are two options that can be specified in your WEB-INF/web.xml file:

...

Scanning for web application resources and TLD scanning are not affected by these options.

Remove unnecessary JARs

Remove any JAR files you do not need. When searching for classes every JAR file needs to be examined to find the needed class. If the jar file is not there - there is nothing to search.

See also a chapter in Tomcat 7 migration guideNote that a web application should never have its own copy of Servlet API or Tomcat classes. All those are provided by the container (Tomcat) and should never be present in the web application. If you are using Apache Maven, such dependencies should be configured with <scope>provided</scope>. See also a stackoverflow page.

Exclude JARs from scanning

In Tomcat 7 JAR files can be excluded from scanning by listing their names or name patterns in a system property. Those are usually configured in the conf/catalina.properties file.

...

Trim the config files as much as possible. XML parsing is not cheap. The less there is to parse - the faster things will go.

...

Web application

  1. Remove any webapps you don't web applications that you do not need. (So remove the all the webapps web applications installed with tomcat) 2. Make sure your code is not doing slow things. (Use a profiler)

...