input.jspで打ち込んだテキストを配列に入れてhome.jspの方で呼び出したいのですがhome.jspのarrayName[index]で「式の型は配列型で List に解決済みのものである必要があ
ります」というエラーが出てしまいます。
どうすればいいですか?
home.jsp
JSP
1<?xml version="1.0" encoding="UTF-8" ?> 2<%@ page import="java.util.ArrayList" %> 3<%@ page import="java.util.List" %> 4<%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 7<html xmlns="http://www.w3.org/1999/xhtml"> 8<head> 9<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 10<title>input</title> 11</head> 12<body> 13 <h1>Go input.jsp</h1> 14 <hr/> 15 <a href="input.jsp">Link</a> 16 17 <% ArrayList<String> arrayName = new ArrayList<String>(); 18 arrayName = (ArrayList<String>)request.getAttribute("name"); 19 if(arrayName != null && !arrayName.isEmpty()){ 20 for(int index = 0; index < arrayName.size(); index++){ 21 %> 22 23 <%= arrayName[index] %> 24 25 <% 26 } 27 } 28 %> 29</body> 30</html>
input.jsp
JSP
1<?xml version="1.0" encoding="UTF-8" ?> 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 8<title>input</title> 9</head> 10<body> 11 <h1>input</h1> 12 <form action="test" method="post"> 13 <input type="text" name="name" value=""/> 14 <input type="submit" value="send"/> 15 </form> 16</body> 17</html>
Test.java(input.jspで入力したテキストを受け取りnameに格納します)
Servlet
1package zTest; 2package zTest; 3 4import java.io.IOException; 5import java.util.ArrayList; 6 7import javax.servlet.RequestDispatcher; 8import javax.servlet.ServletException; 9import javax.servlet.annotation.WebServlet; 10import javax.servlet.http.HttpServlet; 11import javax.servlet.http.HttpServletRequest; 12import javax.servlet.http.HttpServletResponse; 13 14/** 15 * Servlet implementation class Test 16 */ 17@WebServlet("/test") 18public class Test extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 ArrayList<String> name = new ArrayList<String>(); 22 23 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 request.setCharacterEncoding("UTF-8"); 25 String paramName = request.getParameter("input"); 26 name.add(paramName); 27 request.setAttribute("name", name); 28 29 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home.jsp"); 30 dispatcher.forward(request, response); 31 } 32 33} 34
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。