1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2024-10-29

■完成画面

・Eclipse使用

1:入力画面(insert.jsp)

スクリーンショット (106).png
2:処理(ShainInsertComplete.java)
スクリーンショット (107).png

■ソースコード

・insert.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


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);
	}

}


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?