liveBook · Manning

WAR file

As explained in Spring in Action, Sixth Edition

A WAR file is a Java Web Application Archive containing the resources and metadata needed to deploy a web application to a servlet container.

A WAR file packages a Spring Boot web application for deployment to a traditional Java application server. Unlike an executable JAR, it includes the servlet initialization needed for the server to start Spring MVC. section 18.1 section 18.4

Choose the archive format according to the runtime environment:

  • Use a WAR when deploying to Tomcat, WebSphere, WebLogic, or another traditional Java application server. section 18.1
  • Prefer an executable JAR for a Platform as a Service cloud, because its format is simpler than WAR. section 18.1
  • Use a WAR when JSP requires deployment into a traditional servlet container; executable JAR packaging cannot satisfy that requirement. section 2.5

A Spring Boot WAR needs a servlet initializer so the application server can configure Spring’s DispatcherServlet. SpringBootServletInitializer supplies this integration and can bind application filters, servlets, and servlet context initializers. section 18.4

Listing 18.1 Enabling Spring web applications via Java
package tacos;
 
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
 
public class TacoCloudServletInitializer
       extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(
                                  SpringApplicationBuilder builder) {
    return builder.sources(TacoCloudApplication.class);
  }
}

This initializer listing shows the recommended Java configuration: extend SpringBootServletInitializer and return the main application class from configure(). section 18.4

Building and running a WAR

Configure Maven with <packaging>war</packaging> or apply Gradle’s war plugin, then build with mvnw package or gradlew build. The resulting WAR can also be launched with java -jar when supported by the Spring Boot setup. section 18.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains runtime selection, servlet initialization, build configuration, and commands, but does not detail the complete archive layout.
Examples
Uses Tomcat, WebSphere, WebLogic, JSP, Maven packaging, Gradle’s war plugin, and servlet-initializer code.
What it assumes
Assumes a Spring Boot application and basic Maven or Gradle configuration; teaches servlet initialization for server deployment.
What you can do after
Builds a deployable WAR with Maven or Gradle and configures Spring Boot to run inside a traditional Java application server.
How it connects
Connects WAR files to Spring Boot, DispatcherServlet, servlet containers, JSP, executable JARs, and traditional Java application servers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.