WAR Packaging, Provided Scope & Embedded Tomcat
Master WAR deployment models, Maven provided dependency scope to prevent classloader collisions, external Tomcat management, and Spring Boot embedded container mechanics.
WAR Packaging, Provided Scope & Embedded Tomcat
Java web applications can be deployed either using traditional Web Application Archives (WARs) hosted inside an external Tomcat server or via self-contained executable JARs with embedded Tomcat containers in Spring Boot.
1. Web Application Archive (WAR) Packaging
A WAR (.war) is a standardized Java zip archive structured specifically for Servlet Containers:
servlet-crud.war
├── META-INF/
├── WEB-INF/
│ ├── web.xml ──► Deployment Descriptor (URL-to-Servlet Mappings)
│ ├── classes/ ──► Compiled application .class files
│ └── lib/ ──► Third-party JAR dependencies
└── index.html, static assetsDeploying to External Tomcat
- Build the WAR artifact:
mvn clean package──► generatestarget/myapp.war - Copy
myapp.warinto Tomcat'swebapps/folder:cp myapp.war ~/tools/apache-tomcat/webapps/ - Tomcat auto-extracts
myapp.warinto an exploded directorywebapps/myapp/ - Access via Context Path URL:
http://localhost:8080/myapp/hello
2. The Maven <scope>provided</scope> Rule
When developing Servlets for external Tomcat, your code imports classes like jakarta.servlet.http.HttpServlet.
<!-- Maven Dependency in pom.xml -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope> <!-- CRITICAL -->
</dependency>Why <scope>provided</scope> Is Mandatory
WITHOUT PROVIDED SCOPE (Default compile scope):
Maven packages servlet-api.jar INSIDE the WAR (WEB-INF/lib/servlet-api.jar).
Runtime Result: External Tomcat already has servlet-api.jar in its /lib folder!
──► Classloader Conflict! (ClassCastException, LinkageError, unpredictable crashes)
WITH PROVIDED SCOPE:
Maven uses servlet-api.jar ONLY for compilation.
Maven EXCLUDES servlet-api.jar from the packaged WAR.
──► Clean Runtime! External Tomcat supplies its own container Servlet API classes.[!TIP] Dependency Ownership Rule: When a runtime container (Tomcat) provides an infrastructure API, mark the Maven dependency as
providedso it compiles successfully without bundling duplicate container classes into your WAR.
3. External Tomcat vs Embedded Tomcat in Spring Boot
TRADITIONAL EXTERNAL TOMCAT:
"Put the application INSIDE the server"
Application (.war) ──► Copy to External Tomcat Server ──► Start Tomcat
SPRING BOOT EMBEDDED TOMCAT:
"Put the server INSIDE the application"
Embedded Tomcat (.jar) ──► Embedded in Spring Boot App ──► java -jar app.jar| Dimension | Traditional External Tomcat | Spring Boot Embedded Tomcat |
|---|---|---|
| Server Installation | Installed separately on OS host | Bundled inside application JAR via spring-boot-starter-web |
| Deployment Artifact | .war (Web Application Archive) | .jar (Executable Java Archive) |
| Execution Command | ./catalina.sh run or ./startup.sh | java -jar myapp.jar |
| Lifecycle Control | Host administrator controls Tomcat process | Spring Boot main() starts embedded Tomcat on boot |
4. External Tomcat Administration & Port Troubleshooting
Starting & Stopping External Tomcat
# Linux / macOS
cd ~/apache-tomcat-10.1.x
chmod +x bin/*.sh
./bin/startup.sh # Run in background
./bin/shutdown.sh # Graceful shutdown
# Windows
cd C:\apache-tomcat-10.1.x
bin\startup.bat
bin\shutdown.batPort 8080 Collision Resolution
If Tomcat fails to start due to Port 8080 already in use:
# Linux / macOS: Find PID on port 8080 and terminate
lsof -i :8080
kill -9 <PID>
# Windows: Find PID on port 8080 and terminate
netstat -ano | findstr :8080
taskkill /PID <PID> /F❓ Knowledge Check
What happens if you omit <scope>provided</scope> for the Servlet API when building a WAR for external Tomcat?
What is the key architectural shift introduced by Spring Boot embedded Tomcat?
Servlet Lifecycle, Concurrency & Request Pipeline
Master the Servlet lifecycle pipeline (init, service, destroy), multithreaded single-instance concurrency hazards, and request/response abstractions.
DispatcherServlet, Spring MVC & Production Pitfalls
Master the architectural progression from raw Servlets to Spring MVC DispatcherServlet, complete request sequence flows, production pitfalls, and self-assessments.