Jetty ã® Mavenãã©ã°ã¤ã³ã使ã£ã¦ããµã¼ãã¬ããã¨JSPãåä½ãããæ¹æ³ãæ¸ãã¦ããã¾ãã
ãã¼ã¸ã§ã³
使ç¨ãã製åã®ãã¼ã¸ã§ã³ã¯ä»¥ä¸ã®éãã§ãã
ç®æ¬¡
- ãã£ã¬ã¯ããªé層ã®ä½æ
- pom.xml ã®ä½æ
- ãµã¼ãã¬ããã®ä½æ
- JSPã®ä½æ
- åä½ç¢ºèª
1. ãã£ã¬ã¯ããªé層ã®ä½æ
以ä¸ã®ã³ãã³ãã§ããµã³ãã«ããã¸ã§ã¯ã sample-servlet ã®ãã£ã¬ã¯ããªé層ã使ãã¾ãã
mkdir sample-servlet cd sample-servlet mkdir src\main\java mkdir src\main\webapp\WEB-INF
ãµã¼ãã¬ãã㯠src/main/java é
ä¸ã«ç½®ãã¦ãJSP㯠src/main/webapp é
ä¸ã«ç½®ãã¾ãã
2. pom.xml ã®ä½æ
ãã©ã«ã sample-servlet ã« pom.xml ã使ãã¾ãã
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sample-servlet</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>11.0.15</version>
<configuration>
<scan>1</scan>
</configuration>
</plugin>
</plugins>
</build>
</project>
以ä¸ã®è¨å®ã§ãjetty-maven-plugin ã®ããããããã¤ãæå¹ã«ãã¦ãã¾ãã
<configuration> <scan>1</scan> </configuration>
3. ãµã¼ãã¬ããã®ä½æ
åä½ç¢ºèªç¨ã®ã¯ã©ã¹ã使ãã¾ãã
src/main/java/org/example/HelloServlet.java
package org.example; import java.io.IOException; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @WebServlet("/hello") @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.print("Hello, "); System.out.print(req.getParameter("name")); System.out.println("."); } }
4. JSPã®ä½æ
åä½ç¢ºèªç¨ã® JSPã使ãã¾ãã
src/main/webapp/hello.jsp
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<p>Hello, <%= request.getParameter("name") %>.</p>
</body>
</html>
5. åä½ç¢ºèª
以ä¸ã®ã³ãã³ãã§ã³ã³ãããèµ·åãã¾ãã
sample-servlet> mvn jetty:run
5.1. ãµã¼ãã¬ããã®ç¢ºèª
èµ·åå¾ã«ãã©ã¦ã¶ã§ä»¥ä¸ã® URL ãéãã¾ãã
http://localhost:8080/hello?name=Tom
ã³ãã³ãã©ã¤ã³ã«ä»¥ä¸ã®æååã表示ããã¾ãã
Hello, Tom.
5.2. JSPã®ç¢ºèª
ãã©ã¦ã¶ã§ä»¥ä¸ã® URL ãéãã¾ãã
http://localhost:8080/hello.jsp?name=Tom
ãã©ã¦ã¶ã«ä»¥ä¸ã®æååã表示ããã¾ãã
Hello, Tom.
é¢é£è¨äº
以ä¸ã®è¨äºã§ã¯ãJetty Maven Plugin ã®ããããããã¤ã«ã¤ãã¦è©³ããè¨è¼ãã¦ãã¾ãã
Jetty Maven Plugin ããããããã¤ãæå¹ã«ãã¦ã¢ããªãèµ·å