Last Modified 5.5.2025
Jetty Maven Plugin
With the Jetty Maven Plugin, you can test web apps without installing a server.
Create a webapp archetype.
mvn archetype:generate -Dfilter=maven-archetype-webapp - - - - - - - - - 1: remote -> com.haoxuer.maven.archetype:maven-archetype-webapp (a simple maven archetype) 2: remote -> com.lodsve:lodsve-maven-archetype-webapp (Lodsve Maven Archetype Webapp) 3: remote -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.) 4: remote -> org.bytesizebook.com.guide.boot:maven-archetype-webapp (An archetype to create the starting code for the first three chapters of Guide to Web Development with Java, 2nd edition.) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 3 - - - - - - - - - 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.3 7: 1.4 8: 1.5 Choose a number: 8:Just Enter - - - - - - - - - Define value for property 'groupId': : net.java_school.servlet Define value for property 'artifactId': : initParam Define value for property 'version': 1.0-SNAPSHOT: : Just Enter Define value for property 'package': net.java_school.hello: : Just Enter
Create an initParamServlet.java in the example directory.
cd initParam mkdir -p src/main/java/example
initParam
└── src
└── main
└── java
└── example
└── InitParamServlet.java
InitParamServlet.java
package example;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class InitParamServlet extends HttpServlet {
private String url;
private String user;
private String passwd;
private String driver;
@Override
public void init() throws ServletException {
url = this.getInitParameter("url");
user = this.getInitParameter("user");
passwd = this.getInitParameter("passwd");
driver = this.getInitParameter("driver");
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
resp.setContentType("text/html; charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("url: " + url + "<br />");
out.println("user: " + user + "<br />
out.println("passwd: " + passwd + "<br />
out.println("driver: " + driver);
}
}
Modify web.xml as follows.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_1.xsd"
version="6.1"
metadata-complete="true">
<servlet>
<servlet-name>InitParamServlet</servlet-name>
<servlet-class>example.InitParamServlet</servlet-class>
<init-param>
<param-name>driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@127.0.0.1:1521:XE</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>scott</param-value>
</init-param>
<init-param>
<param-name>passwd</param-name>
<param-value>tiger</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitParamServlet</servlet-name>
<url-pattern>/initParam</url-pattern>
</servlet-mapping>
</web-app>
Modify pom.xml as follows.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.java_school.servlet</groupId>
<artifactId>initParam</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>initParam Maven Webapp</name>
<url>http://www.java-school.net</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.web/jakarta.servlet.jsp.jstl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>initParam</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>11.0.25</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Go to the root directory and run the Jetty server with the following command:
mvn jetty:run
Visit http://localhost:8080/initParam.
References