Last Modified 2021.5.7
제티 메이븐 플러그인
제티 메이븐 플러그인(Jetty Maven Plugin)을 사용하면 서버 설치없이 웹 앱을 테스트할 수 있다.
메이븐 아키타입을 생성한다.
C:\ Command Promptmvn archetype:generate -Dfilter=maven-archetype-webapp
C:\ Command Prompt
C:\ Command Prompt
Choose archetype: 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.) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 3
Define value for property 'groupId': : net.java_school.servlet Define value for property 'artifactId': : initParam Define value for property 'version': 1.0-SNAPSHOT: : Define value for property 'package': net.java_school.servlet: :
아래처럼 디텍터리를 생성한 후, initParamServlet.java 파일을 생성한다.
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); } }
web.xml 파일을 수정한다.
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>
pom.xml 파일을 수정한다.
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>
initParam 디렉터리로 이동하여 다음 명령으로 제티 서버를 실행한다.
C:\ Command Promptmvn jetty:run
http://localhost:8080/initParam 을 방문하여 테스트한다.
자바 8에서 테스트하려면 pom.xml과 web.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>
<?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>참고