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

[サーブレット&JSP]ArrayListを使って文字を表示させる

Posted at

formで入力した値をArrayListに格納して表示させる。

form入力

スクリーンショット 2022-06-25 16.29.41.png

入力結果

スクリーンショット 2022-06-25 16.31.01.png

form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="Main" method="post">
      <p>名前を入力してください<p/>
        <input type="text" name="name"><br>
        <input type="text" name="name"><br>
        <input type="text" name="name"><br>
        <input type="text" name="name"><br>
        <input type="text" name="name"><br>
  
   <input type="submit" value="送信">
 </form>

</body>
</html>
User.java
package beans;

public class User {
	
	private String name;

	public User(String name) {
			super();
			this.name = name;
		}

	public User() {
		
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	  }

}

Main.java
package サーブレット;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.User;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String [] name = request.getParameterValues("name");
		ArrayList<User> ArrayList = new ArrayList<>();
		for(int i = 0; i < name.length; i++) {
			User u = new User();
			u.setName(name[i]);
			ArrayList.add(u);
		}
		
		
		request.setAttribute("userlist", ArrayList);
		
		RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
		rd.forward(request, response);
	}

}

doPostでformで入力された値を受け取る

  • formから入力された値を配列値で取得したいので、getParameterValuesメソッドを使用する。
  • ArrayListを宣言
  • for文でname変数の長さ文だけ回す。
  • Userインスタンスを生成し、セッターにname変数をセットする
  • ArrayListにuserインスタンスをaddして、リクエストスコープにセットする
  • display.jspにフォワード
display.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>)request.getAttribute("userlist");%>
   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<% for(int i = 0; i < userList.size(); i++) { %>
<%= userList.get(i).getName()%>

<% } %>


</body>
</html>
1
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
1
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?