プルダウンから小林を選択する
送信ボタンをクリックすると、小林が表示される
purudaun.jsp
<form action="search" method="post">
<div>
<select name="miyoji">
<option value="">選択してください</option>
<% for(int i = 0; i < userList.size(); i++) { %>
<option value="<%= userList.get(i).getName()%>"><%= userList.get(i).getName()%></option>
<% } %>
</select>
</div>
<input type="submit" name="submit" value="送信" />
</form>
search.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String name= request.getParameter("miyoji");
//int number = Integer.parseInt(name);
HttpSession session = request.getSession();
ArrayList<User> userlist = (ArrayList<User>)session.getAttribute("userlist");
ArrayList<User> searchList = new ArrayList<>();
for(int i = 0; i < userlist.size(); i++) {
if(userlist.get(i).getName().contains(name)) {
searchList.add(userlist.get(i));
}
}
session.setAttribute("userlist",searchList);
RequestDispatcher rd = request.getRequestDispatcher("search.jsp");
rd.forward(request, response);
search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="beans.User,java.util.ArrayList" %>
<%ArrayList<User> userList = (ArrayList<User>)session.getAttribute("userlist");%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>プルダウンで選んだ値</p>
<% for(int i = 0; i < userList.size(); i++) { %>
<%= userList.get(i).getName()%>
<% } %>
</body>
</html>