Maven で Web アプリを作成

こちらの記事と同じ事を行いました。
[Java MavenでWeb Applicationを作成する](https://lifehack.world/java-maven-web-application/)

## 使ったソフト

Java のバージョン

```text
$ java --version
openjdk 20.0.2 2023-07-18
OpenJDK Runtime Environment (build 20.0.2+9)
OpenJDK 64-Bit Server VM (build 20.0.2+9, mixed mode, sharing)
```

Maven のバージョン

```text
$ mvn --version
Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/maven
Java version: 20.0.2, vendor: N/A, runtime: /usr/lib/jvm/java-20-openjdk
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "6.4.6-arch1-1", arch: "amd64", family: "unix"
```

## ファイルの用意

```bash
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
```

インターアクティブの操作

```text
Define value for property 'groupId': org.example
Define value for property 'artifactId': web01
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' org.example: : org.example.web
```

pom.xml を次のものと差し替える

```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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>mvn-web-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mvn-web-app Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>20.0</maven.compiler.source>
    <maven.compiler.target>20.0</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>mvn-web-app</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <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.13</version>
  <configuration>
  <scanIntervalSeconds>10</scanIntervalSeconds>
  <connectors>
    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
      <port>8080</port>
      <maxIdleTime>60000</maxIdleTime>
    </connector>
  </connectors>
</configuration>
        </plugin>

 </plugins>
    </pluginManagement>
  </build>
</project>
```

## パッケージの作成

```bash
mvn package
```

## サーバーの起動

```bash
mvn jetty:run
```

## クライアントでアクセス

```text
$ http http://localhost:8080
HTTP/1.1 200 OK
Content-Length: 52
Content-Type: text/html;charset=utf-8
Date: Sat, 29 Jul 2023 04:32:17 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Server: Jetty(11.0.13)
Set-Cookie: JSESSIONID=node0rlyh2t6mm5holpmqesfmvht90.node0; Path=/

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
```

ブラウザーで　http://localhost:8080/　 にアクセス
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/31aecf28-3c6b-f4bd-8d6f-8465905d3cc0.png)

## 日本語を使う

index.jsp を次のものと差し替えます。

```jsp:src/main/webapp/index.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<h2>Hello World!</h2>
<p>こんにちは</p>
<p>Jul/30/2023 AM 08:59</p>
</body>
</html>
```

ブラウザーでアクセス
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/bd08886c-cccb-0831-c535-41c7aefc79b8.png)

## 時刻の表示

index.jsp を次のものと差し替えます。

```jsp:src/main/webapp/index.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<h2>Hello World!</h2>
<p>こんにちは</p>
<blockquote>
日時：<%= new java.util.Date() %>
</blockquote>
<p>Jul/30/2023 AM 08:59</p>
</body>
</html>
```

ブラウザーでアクセス
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/89213040-6c2c-bf48-defd-8cf428299c19.png)