java
1コード index.jsp 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4<%@ page import=" java.util.ArrayList "%> 5<%@ page import=" java.util.List"%> 6<%@ page import=" sample1.UserDto"%> 7 8<jsp:useBean id="userDto" class="sample1.UserDto" scope="request" /> 9<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10<html> 11 <head> 12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 13 <title>Insert title here</title> 14 </head> 15 <boby> 16 17 <% 18 List<UserDto> list = (List<UserDto>) request.getAttribute("userDto"); 19 20 for(UserDto a : list){ 21 22 23 out.println(a.getid()); 24 out.println(a.getname()); 25 out.println(a.getage()); 26 } 27 28 %> 29 </boby> 30</html>
java
1コード UserDto.java 2package sample1; 3 4public class UserDto { 5 6 7 private int id; 8 private String name; 9 private int age; 10 11 12 13 public int getid(){ 14 return id; 15 } 16 17 public void setid(int id){ 18 this.id = id; 19 } 20 21 public String getname(){ 22 return name; 23 } 24 25 public void setname(String name){ 26 this.name = name; 27 } 28 29 public int getage(){ 30 return age; 31 32 } 33 34 public void setage(int age){ 35 this.age = age; 36 37 } 38 39 40 41} 42
java
1コード Hello.java 2package sample1; 3 4import java.io.IOException; 5import java.io.PrintWriter; 6import java.sql.Connection; 7import java.sql.DriverManager; 8import java.sql.ResultSet; 9import java.sql.SQLException; 10import java.sql.Statement; 11import java.util.ArrayList; 12import java.util.List; 13 14import javax.servlet.RequestDispatcher; 15import javax.servlet.ServletException; 16import javax.servlet.annotation.WebServlet; 17import javax.servlet.http.HttpServlet; 18import javax.servlet.http.HttpServletRequest; 19import javax.servlet.http.HttpServletResponse; 20 21/** 22 * SELECT文を発行・画面に表示するクラス 23 * 24 * @author mori 25 * 26 */ 27@WebServlet("/Hello") 28public class Hello extends HttpServlet { 29 private static final long serialVersionUID = 1L; 30 31 /** 32 * 33 * SELECT文を発行・画面に表示するメソッド 34 * 35 * @param request 36 * サーブレットへのリクエスト 37 * @param response 38 * サーブレットからのレスポンス 39 * @throws ServletException 40 * 処理中に問題が起こったときに、Servlet がスローする可能性がある例外。 41 * @throws IOException 42 * 入出力処理の失敗、または割り込みの発生によって生成される例外 43 * 44 */ 45 protected void doGet(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 response.setContentType("text/html; charset=utf-8"); 48 PrintWriter out = response.getWriter(); 49 50 out.println("<html>"); 51 52 out.println("<body>"); 53 54 Connection conn = null; 55 String url = "jdbc:mysql://localhost/training"; 56 String user = "root"; 57 String password = "ryota"; 58 59 try { 60 Class.forName("com.mysql.jdbc.Driver"); 61 conn = DriverManager.getConnection(url, user, password); 62 63 Statement stmt = conn.createStatement(); 64 String sql = "SELECT * FROM user"; 65 ResultSet rs = stmt.executeQuery(sql); 66 67 List<UserDto> users = new ArrayList<>(); 68 69 while (rs.next()) { 70 UserDto dto = new UserDto(); 71 72 dto.setid(rs.getInt("id")); 73 dto.setname(rs.getString("name")); 74 dto.setage(rs.getInt("age")); 75 users.add(dto); 76 77 } 78 79 80 81 82 83 request.setAttribute("userDto", users); 84 85 86 String jsp = "/WEB-INF/index.jsp"; 87 88 // ServletContext sc = getServletContext(); 89 90 91 RequestDispatcher dispatch = request.getRequestDispatcher(jsp); 92 93 dispatch.forward(request, response); 94 95 96 97 98 rs.close(); 99 stmt.close(); 100 } catch (ClassNotFoundException e) { 101 out.println("lassNotFoundException:" + e.getMessage()); 102 } catch (SQLException e) { 103 out.println("SQLException:" + e.getMessage()); 104 } catch (Exception e) { 105 out.println("Exception:" + e.getMessage()); 106 } finally { 107 108 try { 109 if (conn != null) { 110 conn.close(); 111 } 112 } catch (SQLException e) { 113 out.println("SQLException:" + e.getMessage()); 114 } 115 116 } 117 118 out.println("</body>"); 119 out.println("</html>"); 120 } 121 122 /** 123 * doPostメソッド 124 * 125 */ 126 127 protected void doPost(HttpServletRequest request, HttpServletResponse response) 128 throws ServletException, IOException { 129 doGet(request, response); 130 } 131}
Hello.javaの方から配列を渡してそれを,jsp 側で表示させたいのですが
Exception:java.lang.ClassCastException: java.util.ArrayList cannot be cast to sample1.UserDto
とエラーが出てしまいます。
回答1件
あなたの回答
tips
プレビュー