Sub topic 8
When you need a WAR: deploying to an external servlet container
This chapter assumes you can build a Spring Boot application with Maven or Gradle, and that you know how the executable JAR from the earlier chapters works. You should also know what a servlet container is: a server such as Tomcat or Jetty that loads web applications and routes HTTP requests to them.
JAR or WAR: the decision
A Spring Boot executable JAR contains its own embedded servlet container. You run it with java -jar, and the application owns the server. A WAR (Web Application Archive) reverses that relationship. You hand the archive to a servlet container that already runs on the machine, and the container owns the server and the process.
For a new application, the JAR is almost always the better choice. It has fewer moving parts, behaves the same on a laptop and in production, and maps cleanly onto systemd and container images. Choose a WAR only when a constraint forces it.
| Situation | Better choice |
|---|---|
| New service, deployed as a container or systemd unit | Executable JAR |
| Organization runs a shared Tomcat or Jetty cluster and requires applications to be deployed into it | WAR |
| Operations team manages container versions, TLS, and connectors centrally | WAR |
| Application server features such as container-managed JNDI data sources are mandated | WAR |
| You need one archive that works both ways | Executable WAR |
Typical reasons for a WAR are organizational, not technical. An operations team may patch and monitor one set of Tomcat servers and refuse to run arbitrary java -jar processes. A licensing or compliance rule may require a specific application server. A legacy deployment pipeline may only know how to copy a .war file into a directory.
How a WAR starts a Spring Boot application
In a JAR, the main method starts Spring and the embedded server. In a WAR, the container starts first. It scans the archive for a class that implements jakarta.servlet.ServletContainerInitializer (Servlet 5 and later, which is what Spring Boot 3 uses). Spring provides one, and it looks for a SpringBootServletInitializer subclass in your application. That class builds the Spring ApplicationContext and attaches it to the servlet context the container supplied.
Because the container, not main, drives startup, the main method is not used when the WAR is deployed to an external container. Keeping it is still useful, because it lets you run the same application from your IDE or with java -jar during development.
Step 1: extend SpringBootServletInitializer
Make your main application class extend SpringBootServletInitializer and override configure.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}The configure method tells Spring which configuration class to use when the container starts the application. Without this subclass, the container deploys the WAR, finds nothing to start, and serves 404 for every request. This is the most common WAR mistake.
Step 2: change the packaging
With Maven, set the packaging type in pom.xml.
<packaging>war</packaging>With Gradle, apply the war plugin next to the Spring Boot plugin.
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
}Adjust the Spring Boot version to the one your project already uses. After this change, the build produces target/myapp-1.0.0.war (Maven) or build/libs/myapp-1.0.0.war (Gradle). Gradle's bootWar task builds the executable version, and war builds a plain one.
Step 3: mark the embedded container as provided
If the WAR keeps the embedded Tomcat on its runtime classpath, you end up with two servlet containers in one process: the external one and the one inside WEB-INF/lib. This causes class conflicts and confusing startup errors. Tell the build that the external container supplies the server classes.
With Maven, add the Tomcat starter with provided scope after the web starter.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>With Gradle, use the providedRuntime configuration.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}Spring Boot moves provided dependencies into WEB-INF/lib-provided in the archive. That keeps them out of the container's class loader but still lets the archive run with java -jar.
Executable WAR: one archive, two ways to run
A Spring Boot WAR built with the plugin is both a deployable WAR and an executable archive. If you leave the Tomcat starter in provided scope, the same file works in two modes:
# Maven
./mvnw clean package
# Run it directly, using the classes in WEB-INF/lib-provided
java -jar target/myapp-1.0.0.war
# Or deploy it to an external Tomcat
cp target/myapp-1.0.0.war /opt/tomcat/webapps/This is useful during a migration. A team can deploy to the shared container today and move to java -jar or containers later without changing the artifact format again.
Match the container to your Spring Boot version
The container must support the servlet API version your Spring Boot version expects. A mismatch is a frequent source of deployment failures.
| Spring Boot | Servlet namespace | Minimum container |
|---|---|---|
| 2.x | `javax.servlet` | Servlet 3.1 containers such as Tomcat 8.5 or 9 |
| 3.x | `jakarta.servlet` | Servlet 5.0 or 6.0 containers such as Tomcat 10.1 or Jetty 12 |
Spring Boot 3 requires Java 17 or later, so the JVM running the container must also be Java 17 or later. Deploying a Spring Boot 3 WAR to Tomcat 9 fails at startup with ClassNotFoundException or NoClassDefFoundError for jakarta.servlet classes, because Tomcat 9 only provides javax.servlet.
Behavior that changes in a WAR
Several settings you rely on in a JAR no longer apply, because the container controls the server.
- Port and connectors.
server.port,server.ssl.*, andserver.tomcat.*properties have no effect. The container's ownserver.xml(Tomcat) defines the port, TLS, and thread pool. - Context path.
server.servlet.context-pathis ignored. The context path comes from the WAR file name.myapp.waris served at/myapp. To serve at the root, name the fileROOT.war, or configure the container. - Graceful shutdown.
server.shutdown=gracefulapplies to embedded servers. In an external container, shutdown behavior depends on how the container is stopped and how it handles in-flight requests. Check the container's documentation for its stop timeout. - Process and JVM options. Heap size and other JVM flags belong to the container's startup script, for example
CATALINA_OPTSorsetenv.shfor Tomcat. All applications in that container share one JVM and one heap. - Configuration. Profiles and environment variables still work, but you set them in the container's environment. For Tomcat, put
SPRING_PROFILES_ACTIVEinsetenv.shso it is available to the JVM.
# $CATALINA_BASE/bin/setenv.sh
export SPRING_PROFILES_ACTIVE=prod
export DB_URL=jdbc:postgresql://db.internal:5432/app
export CATALINA_OPTS="-Xms512m -Xmx1g"Keep secrets out of setenv.sh when possible, and restrict its file permissions, as covered in the chapter on handling secrets.
Because several applications can share one JVM, a memory leak or a runaway thread in one application affects the others. This is a real cost of the WAR model, and a reason that many teams prefer one process per application.
Common errors
- 404 for every URL after deployment. The class extending
SpringBootServletInitializeris missing, or itsconfiguremethod does not name your application class. ClassNotFoundException: jakarta.servlet.... The container is too old for Spring Boot 3. Upgrade to Tomcat 10.1 or later.- Duplicate servlet container errors, or
LinkageError. The embedded Tomcat starter is not markedprovided. Fix the scope and rebuild. - Application works at
/myappbut links point to/. The context path comes from the file name. Use relative links, or deploy asROOT.war. - Settings ignored. You set
server.portorserver.servlet.context-pathinapplication.properties. These do not apply in an external container. - Redeploy leaks memory. Hot redeploys in a long-running container can leave old class loaders alive, so restarting the container is more reliable than repeated hot redeployment for production.
Verify the deployment
After copying the WAR, check that the container deployed it and that Spring started.
cp target/myapp-1.0.0.war /opt/tomcat/webapps/myapp.war
tail -f /opt/tomcat/logs/catalina.outLook for the Spring Boot banner and a line such as Started Application in 4.2 seconds. Then request an endpoint with the context path included.
curl -i http://localhost:8080/myapp/actuator/healthIf you enabled Actuator health endpoints, the response should be 200 with {"status":"UP"}. The port is the container's port, not a value from your application properties.
Exercise
A team builds a Spring Boot 3 application as a WAR named inventory.war and deploys it to a shared Tomcat 9 server. Tomcat starts, but the application fails with a ClassNotFoundException for jakarta.servlet classes. Separately, after they fix that, the application answers at /inventory/ but they want it at the root path, and server.servlet.context-path=/ in application.properties has no effect. What causes each problem, and how do you fix each?
Show answer
The first problem is a version mismatch. Spring Boot 3 uses the jakarta.servlet package, and Tomcat 9 only provides javax.servlet. Deploy to Tomcat 10.1 or later, which also requires Java 17 or later. The second problem is that in an external container the context path comes from the WAR file name, and server.servlet.context-path is ignored. Rename the file to ROOT.war, or configure the context path in the container.
Exercise
Your WAR deploys without errors but every request returns 404. The application class has @SpringBootApplication and a main method. What is the most likely missing piece, and what must it contain?
Show answer
The application class does not extend SpringBootServletInitializer. The container never calls main, so it needs a subclass that overrides configure(SpringApplicationBuilder builder) and returns builder.sources(Application.class), which tells Spring which configuration class to start.