eclipseでjavaを使って作成しています。
作成しているプログラムは、webブラウザ上で県名を入力し、DBに格納されている県名と一致していた場合、その県名の[県名、面積、人口]をwebブラウザ上で表示するというものです。
そこで、Object型から.getで取得して表示したいのですがうまくいきません。
ご教授頂けると大変助かります。
発生している問題・エラーメッセージ
PrefectureData preList = (PrefectureData) request.getAttribute("preList"); を List<PrefectureData> preList = (List<PrefectureData>) request.getAttribute("preList"); に修正した結果が以下のエラーです。 タイプ 例外報告 メッセージ java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 説明 サーバーは予期しない条件に遭遇しました。それはリクエストの実行を妨げます。 例外 org.apache.jasper.JasperException: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:582) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:498) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:383) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:331) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) servlet.PrefectureSrv.doPost(PrefectureSrv.java:73) javax.servlet.http.HttpServlet.service(HttpServlet.java:681) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 根本原因 java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) java.base/java.util.Objects.checkIndex(Objects.java:372) java.base/java.util.ArrayList.get(ArrayList.java:458) org.apache.jsp.WEB_002dINF.jsp.PrefectureResult_jsp._jspService(PrefectureResult_jsp.java:136) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:383) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:331) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) servlet.PrefectureSrv.doPost(PrefectureSrv.java:73) javax.servlet.http.HttpServlet.service(HttpServlet.java:681) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
ソースコード
java
1//PrefectureSrv.java 2package servlet; 3 4import java.io.IOException; 5import java.sql.Connection; 6import java.sql.DriverManager; 7import java.sql.PreparedStatement; 8import java.sql.ResultSet; 9import java.sql.SQLException; 10import java.util.ArrayList; 11import java.util.List; 12 13import javax.servlet.RequestDispatcher; 14import javax.servlet.ServletException; 15import javax.servlet.annotation.WebServlet; 16import javax.servlet.http.HttpServlet; 17import javax.servlet.http.HttpServletRequest; 18import javax.servlet.http.HttpServletResponse; 19 20import model.PrefectureData; 21 22/** 23 * Servlet implementation class DataBaseServer 24 */ 25 26@WebServlet("/PrefectureSrv") 27public class PrefectureSrv extends HttpServlet { 28 private static final long serialVersionUID = 1L; 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 32 * response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) 35 throws ServletException, IOException { 36 // フォワード 37 RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/PrefectureInput.jsp"); 38 dispatcher.forward(request, response); 39 } 40 41 /** 42 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 43 * response) 44 */ 45 protected void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 try (// DB接続 48 Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//172.16.4.6:1521/infopdb", "uenoa", "uenoa"); 49 50 ){ 51 52 String name = request.getParameter("name"); 53 54 // データの出力 55 String sql = "select 県名, 面積, 人口 from REVENGE where 県名=?"; 56 PreparedStatement pstmt = con.prepareStatement(sql); 57 pstmt.setString(1, name); 58 ResultSet rs = pstmt.executeQuery(); 59 60 List<PrefectureData> preList = new ArrayList<PrefectureData>(); 61 62 while (rs.next()) { 63 PrefectureData preData = new PrefectureData(); 64 65 preData.setName(rs.getString("県名")); 66 preData.setArea(rs.getDouble("面積")); 67 preData.setPopulation(rs.getDouble("人口")); 68 69 preList.add(preData); 70 71 } 72 73 String error = "入力された県名は正しくありません"; 74 75 if (preList.size() == 0) { 76 request.setAttribute("error", error); 77 RequestDispatcher dispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/PrefectureResult.jsp"); 78 dispatcher1.forward(request, response); 79 } else { 80 request.setAttribute("data", preList); 81 RequestDispatcher dispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/PrefectureResult.jsp"); 82 dispatcher1.forward(request, response); 83 } 84 85 } catch (SQLException e) { 86 System.out.println("データベース接続エラー" + e); 87 } 88 } 89 90} 91 92//PrefectureData.java 93package model; 94 95public class PrefectureData { 96 private String name; 97 private double area; 98 private double population; 99 100 public final String getName() { 101 return name; 102 } 103 104 public void setName(String name) { 105 this.name = name; 106 } 107 108 public final double getArea() { 109 return area; 110 } 111 112 public void setArea(double area) { 113 this.area = area; 114 } 115 116 public final double getPopulation() { 117 return population; 118 } 119 120 public void setPopulation(double population) { 121 this.population = population; 122 } 123} 124 125//PrefectureInput.jsp 126<%@ page language="java" contentType="text/html; charset=UTF-8" 127 pageEncoding="UTF-8"%> 128<%@ page import="javax.servlet.http.*,javax.servlet.*"%> 129 130<!DOCTYPE html> 131<html> 132<head> 133<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 134<title>県名入力</title> 135</head> 136<body> 137 <form method="post" action="./PrefectureSrv"> 138 <div>検索したい県名を入力してください</div> 139 <div> 140 <input type="text" name="name" maxlength="20" size="20"> 141 </div> 142 <input type="submit" value="検索"> 143 </form> 144</body> 145 146</html> 147 148 149//PrefectureResult.jsp 150<%@ page language="java" contentType="text/html; charset=UTF-8" 151 pageEncoding="UTF-8"%> 152<%@ page import="javax.servlet.http.*,javax.servlet.*" 153 import="java.io.*,java.util.*,java.text.*" 154%> 155<%@ page import="model.PrefectureData"%> 156<% 157 request.setCharacterEncoding("UTF-8"); 158 PrefectureData preList = (PrefectureData)request.getAttribute("data"); 159%> 160 161<!DOCTYPE html> 162<html> 163<head> 164<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 165<title>検索結果</title> 166</head> 167<body> 168 <p><%= preList.getName() %></p> 169 <p><%= preList.getArea() %></p> 170 <p><%= preList.getPopulation() %></p> 171</body> 172 173</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/12 07:21 編集
2021/10/12 07:33
2021/10/12 07:55
2021/10/12 08:09
2021/10/12 08:38
2021/10/12 08:53
2021/10/12 09:03
2021/10/13 01:16
2021/10/18 04:37
2021/10/18 05:52
2021/10/18 06:00