Java Jsp サーブレット フォームの値取得（POST）。出力。

### ■完成画面

・Eclipse使用

１：入力画面（insert.jsp）

![スクリーンショット (106).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/478380/e80c779a-4ce2-bdda-5cdf-b60ff572e7da.png)
２：処理（ShainInsertComplete.java）
![スクリーンショット (107).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/478380/76ddeafe-ac07-650a-8b3b-d53b80e2cb96.png)

### ■ソースコード
・insert.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>社員登録画面</title>
<style>
.form-input {
width: 100%;
}

.form-table td {
padding: 5px;
}

.form-table label {
text-align: right;
}

.form-button {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>社員登録画面</h1>
<form action="ShainInsertComplete" method="post">
<table class="form-table">
<tr>
<td><label for="id">ID:</label></td>
<td><input type="text" id="id" name="id" class="form-input"
pattern="\d{3}" required title="IDは3桁の数字で入力してください"></td>
</tr>
<tr>
<td><label for="name">名前:</label></td>
<td><input type="text" id="name" name="name" class="form-input"
required></td>
</tr>
<tr>
<td><label for="sei">姓:</label></td>
<td><select id="sei" name="sei" class="form-input" required>
<option value="">選択してください</option>
<option value="男">男</option>
<option value="女">女</option>
</select></td>
</tr>
<tr>
<td><label for="nen">年:</label></td>
<td>
<select id="nen" name="nen" class="form-input" required>
<option value="">選択してください</option>
]
<%

int NowYear = 2024;
for(int i = 1945; i <= NowYear; i++) {
	
%>

<option value="<%= i %>"><%= i %></option>

<%

}

%>


</select>
</td>
</tr>
<tr>
<td><label for="address">住所:</label></td>
<td><input type="text" id="address" name="address" class="form-input" required></td>
</tr>
</table>
<button type="submit" class="form-button">登録</button>
</form>
</body>
</html>
```

・ShainInsertComplete.java
```java

package info.searchman;

import java.io.IOException;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShainInsertComplete
 */
public class ShainInsertComplete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShainInsertComplete() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 文字コード指定
		request.setCharacterEncoding("UTF-8");
		
		// フォームからパラメータを取得して表示
		System.out.println(request.getParameter("id"));
		System.out.println(request.getParameter("name"));
		System.out.println(request.getParameter("sei"));
		System.out.println(request.getParameter("nen"));
		System.out.println(request.getParameter("address"));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


```