0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Servlet&JSP]プルダウンで選択した値を表示させる。ArrayList

Posted at

プルダウンから小林を選択する

スクリーンショット 2022-07-01 22.16.48.png

送信ボタンをクリックすると、小林が表示される

スクリーンショット 2022-07-01 22.17.26.png

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>
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?