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 run it as an executable JAR. It also assumes you know that a servlet container, such as Tomcat, is the server that receives HTTP requests and passes them to Java web code.
JAR or WAR
A Spring Boot executable JAR embeds its own servlet container. You start it with java -jar, and the application owns the server.
A WAR (Web Application Archive) reverses this. A servlet container that already runs on the machine loads your application. Operations staff manage the container, and several applications can share it.
Choose a WAR only when something outside your control requires it:
- Your organization runs a shared Tomcat or Jetty and will not allow another process type.
- A policy requires the container to be patched and monitored centrally.
- You must deploy into an application server that supplies resources such as managed data sources.
If none of these apply, prefer the executable JAR. It is simpler to run, test, and containerize.
Changing the packaging
In Maven, set the packaging type in pom.xml:
<packaging>war</packaging>In 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'
}The container supplies the servlet API and its own Tomcat classes. Your WAR must not bundle a second copy. Mark the embedded server as provided in Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>In Gradle, use providedRuntime:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}Letting the container start the application
An external container does not call your main method. It looks for a servlet initializer instead. Extend SpringBootServletInitializer and register your application sources:
@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 main method stays, so the same code still runs from an IDE or with the embedded server during development.
Building and deploying
Build as usual:
# Maven
./mvnw clean package
# Gradle
./gradlew clean bootWarCopy the resulting .war file into the container's deployment directory. For Tomcat this is webapps/. Tomcat unpacks the file and starts the application.
cp target/myapp-1.0.0.war /opt/tomcat/webapps/myapp.warA Spring Boot WAR built this way can still be run with java -jar unless you exclude that. This is useful for local testing.
Differences you will notice
The WAR changes how several earlier topics behave.
| Topic | Executable JAR | WAR in external container |
|---|---|---|
| Port | `server.port` | Set in the container's connector configuration. `server.port` is ignored. |
| Context path | `/` by default | The WAR file name, so `myapp.war` serves `/myapp`. |
| Graceful shutdown | `server.shutdown=graceful` | The container controls shutdown and undeploy. |
| JVM options | Command line of your process | The container's startup scripts, such as `CATALINA_OPTS`. |
| Restart | Restart one process | Redeploy or restart the container, which may affect other applications. |
Common errors
- The application starts but returns 404 for every URL. The context path is the WAR file name, so check the URL prefix.
- A
ClassCastExceptionor aLinkageErrorappears at startup. The WAR includes embedded Tomcat classes. Check thatspring-boot-starter-tomcatisprovided. - Configuration files are not found. External containers do not read files from your working directory. Pass settings through container system properties or environment variables.
Exercise
A team deploys a Spring Boot WAR to Tomcat 10.1. They set server.port=9090 in application.properties, but the application still answers on port 8080. Why, and where must they change the port?
Show answer
The container owns the HTTP connector, so Spring Boot ignores server.port when the application runs inside an external Tomcat. They must change the port on the connector in Tomcat's conf/server.xml.