Servlet Deployment
WAR File Structure
A WAR (Web Application Archive) file is a ZIP file with a .war extension that packages a Java web application for deployment. It follows a standard directory structure:
myapp.war
├── index.jsp ← Public web resources
├── about.html
├── css/
│ └── style.css
├── js/
│ └── app.js
├── images/
└── WEB-INF/ ← Protected directory (not accessible by clients)
├── web.xml ← Deployment descriptor
├── classes/ ← Compiled .class files
│ └── com/example/
│ ├── HelloServlet.class
│ └── UserDAO.class
├── lib/ ← JAR dependencies
│ ├── mysql-connector.jar
│ └── jstl.jar
└── views/ ← Protected JSP views
├── login.jsp
└── dashboard.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>My Servlet Application</display-name>
<description>A sample servlet web application</description>
<!-- Context-wide init parameters -->
<context-param>
<param-name>appName</param-name>
<param-value>MyApp</param-value>
</context-param>
<!-- Servlet declarations -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
<init-param>
<param-name>greeting</param-name>
<param-value>Hello</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!-- Filter -->
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>com.example.filters.LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Session configuration -->
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
</session-config>
<!-- Welcome files -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Error pages -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error-404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/views/error-500.jsp</location>
</error-page>
</web-app>
Maven WAR Plugin and Deployment
<build>
<finalName>myapp</finalName>
<plugins>
<!-- Maven WAR Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<warName>myapp</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- Tomcat Maven Plugin (for development) -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/myapp</path>
</configuration>
</plugin>
</plugins>
</build>
<!-- Build: mvn clean package -->
<!-- Run with Tomcat plugin: mvn tomcat7:run -->
<!-- Deploy: copy target/myapp.war to TOMCAT_HOME/webapps/ -->
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.