Servlet deployment is the process of packaging, configuring, and installing a Java web application on a servlet tl-container (like Apache Tomcat, Jetty, or WildFly) so it can handle HTTP requests. Deployment involves creating a WAR file, configuring the deployment descriptor, and deploying it to the server.
Understanding deployment is crucial for moving applications from development to production environments. Proper deployment ensures your servlets are accessible, secure, and performant in real-world scenarios.
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 defined by the Java Servlet specification:
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 simplifies WAR file creation and deployment through the Maven WAR Plugin. This plugin automatically packages your application following the standard WAR structure and can be configured for different deployment scenarios.
The maven-war-plugin handles resource filtering, dependency management, and WAR file generation. Setting failOnMissingWebXml to false allows deployment without web.xml when using annotations.
<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/ -->
There are several ways to deploy a servlet application to a server:
Copy the WAR file to the server's deployment directory. For Tomcat, this is TOMCAT_HOME/webapps/. The server automatically extracts and deploys the application.
# Build WAR file with Maven
mvn clean package
# Copy to Tomcat webapps directory
cp target/myapp.war /opt/tomcat/webapps/
# Tomcat automatically deploys the application
# Access at: http://localhost:8080/myapp/
# To undeploy, simply delete the WAR and extracted folder
rm -rf /opt/tomcat/webapps/myapp*
Use Tomcat's web-based Manager application to deploy, undeploy, start, stop, and reload applications through a GUI or REST API.
<!-- Add to TOMCAT_HOME/conf/tomcat-users.xml -->
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="admin123" roles="manager-gui,manager-script"/>
</tomcat-users>
<!-- Access Manager at: http://localhost:8080/manager/html -->
# Deploy via Tomcat Manager REST API
curl -u admin:admin123 \
--upload-file target/myapp.war \
"http://localhost:8080/manager/text/deploy?path=/myapp&update=true"
# List deployed applications
curl -u admin:admin123 \
"http://localhost:8080/manager/text/list"
# Undeploy application
curl -u admin:admin123 \
"http://localhost:8080/manager/text/undeploy?path=/myapp"
Deploy directly from Maven using the Tomcat Maven Plugin. This is ideal for development and testing environments.
# Run embedded Tomcat server (development)
mvn tomcat7:run
# Deploy to remote Tomcat server
mvn tomcat7:deploy
# Redeploy (update existing deployment)
mvn tomcat7:redeploy
# Undeploy from server
mvn tomcat7:undeploy
# Run on specific port
mvn tomcat7:run -Dmaven.tomcat.port=9090
The web.xml file is the deployment descriptor that configures your web application. While Servlet 3.0+ supports annotations, web.xml is still widely used for centralized configuration.
Servlet 3.0 introduced annotations as an alternative to web.xml configuration. Annotations are placed directly in servlet classes, making configuration more maintainable.
package com.example;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(
name = "HelloServlet",
urlPatterns = {"/hello", "/greet"},
loadOnStartup = 1,
initParams = {
@WebInitParam(name = "greeting", value = "Hello"),
@WebInitParam(name = "language", value = "en")
}
)
public class HelloServlet extends HttpServlet {
private String greeting;
@Override
public void init() throws ServletException {
greeting = getInitParameter("greeting");
System.out.println("HelloServlet initialized with greeting: " + greeting);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
if (name == null || name.isEmpty()) {
name = "World";
}
out.println("<html>");
out.println("<body>");
out.println("<h1>" + greeting + ", " + name + "!</h1>");
out.println("</body>");
out.println("</html>");
}
}
// No web.xml needed! Access at: http://localhost:8080/myapp/hello?name=John
The context path is the URL prefix for your application. If you deploy myapp.war, the context path is /myapp. All servlet URLs are relative to this context path.
| URL Pattern | Matches | Example |
|---|---|---|
/hello |
Exact match | /myapp/hello |
/user/* |
Path prefix | /myapp/user/profile, /myapp/user/settings |
*.do |
Extension match | /myapp/login.do, /myapp/register.do |
/ |
Default servlet | All unmatched requests |
/* |
All requests | Every request (use for filters) |
/shop instead of /app1Cause: Missing JAR files in WEB-INF/lib or incorrect package structure in WEB-INF/classes
Solution: Verify all dependencies are included in the WAR file and class files are in correct package directories
Cause: Incorrect URL pattern, servlet not registered, or wrong context path
Solution: Check servlet mapping in web.xml or @WebServlet annotation, verify context path
Cause: Another application is using port 8080
Solution: Change Tomcat port in server.xml or stop the conflicting application
Cause: Insufficient heap memory for the application
Solution: Increase JVM heap size: export CATALINA_OPTS="-Xms512m -Xmx2048m"
Explore 500+ free tutorials across 20+ languages and frameworks.