Last Modified 2021.11.30
Jetty Maven Plugin
With the Jetty Maven Plugin, you can test web apps without installing a server.
Create a webapp archetype.
C:\ Command Promptmvn 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 Choose a number: 7: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.
C:\ Command Promptcd 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 javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.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="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"> <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> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</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>10.0.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
Go to the root directory and run the Jetty server with the following command:
C:\ Command Promptmvn jetty:run
Visit http://localhost:8080/initParam.
If you are using Java 8, modify pom.xml and web.xml by referring to the following.
pom.xml<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>web.xml
<?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"> </web-app>References