QandAシステムを作成中です。サーブレットからスコープに保存した値を、
JSPで表示するとエラーが発生します。
原因をご教授いただけますでしょうか。
JavaBeans
1package model; 2 3import java.io.Serializable; 4 5 6public class Question implements Serializable { 7 private int question_id; 8 private String handle_name; 9 private String title; 10 private String contents; 11 private int urgency; 12 private String edit_delete_key; 13 private String regist_timestamp; 14 private String update_timestamp; 15 16public Question() {} 17 public Question(int question_id, String handle_name, String title, String contents, int urgency, String edit_delete_key, String regist_timestamp, String update_timestamp) { 18 this.question_id = question_id; 19 this.handle_name = handle_name; 20 this.title = title; 21 this.contents = contents; 22 this.urgency = urgency; 23 this.edit_delete_key = edit_delete_key; 24 this.regist_timestamp = regist_timestamp; 25 this.update_timestamp = update_timestamp; 26 } 27 28public int getQuestion_id(){return question_id;} 29public String getHandle_name(){return handle_name;} 30public String getTitle(){return title;} 31public String getContents(){return contents;} 32public int getUrgency(){return urgency;} 33public String getEdit_delete_key(){return edit_delete_key;} 34public String getRegist_timestamp(){return regist_timestamp;} 35public String getUpdate_timestamp(){return update_timestamp;} 36}
servlet
1package servlet; 2 3import java.io.IOException; 4import java.util.List; 5//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; 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; 13import javax.servlet.http.HttpSession; 14import model.GetLogic; 15import model.Question; 16 17/** 18 * Servlet implementation class Doctest 19 */ 20 21@WebServlet("/RegistQuestionServlet") 22public class ShowQuestionServlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24/** 25* @see HttpServlet#doget(HttpServletRequest request, HttpServletResponse response) 26*/ 27protected void doGet(HttpServletRequest request, HttpServletResponse response) 28throws ServletException, IOException { 29 30//質問リスト取得 31GetLogic getLogic = new GetLogic(); 32List<Question> questionList = getLogic.execute(); 33HttpSession session = request.getSession(); 34session.setAttribute("questionList", questionList); 35 36//フォワード 37RequestDispatcher dispatcher = 38 request.getRequestDispatcher("/WEB-INF/jsp/QandAList.jsp"); 39 dispatcher.forward(request, response); 40} 41}
DAO
1package DAO; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.util.ArrayList; 9import java.util.List; 10 11import model.Question; 12 13 14 15public class QuestionDAO { 16// JDBC読み込み 17static{ 18 try{Class.forName("org.postgresql.Driver"); 19 } catch(ClassNotFoundException e) { 20 e.printStackTrace(); 21 } 22} 23 24public List<Question> findAll() { 25List<Question> questionList = new ArrayList<>(); 26// データベースに使用する情報 27String jdbc_url ="jdbc:postgresql://localhost:5432/QandA"; 28String user = "postgres"; 29String pass = "postgres"; 30//Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis() - 1000*60*60*24); 31 // データベース接続 32 try(Connection conn = DriverManager.getConnection(jdbc_url, user, pass)){ 33 34 //SELECT文の準備 35 String sql = "select * from question"; 36 PreparedStatement pStmt = conn.prepareStatement(sql); 37 38 //SELECT文を実行し結果を取得 39ResultSet rs = pStmt.executeQuery(); 40 41//SELECT文の結果を取り出す 42while (rs.next()) { 43 int question_id = rs.getInt("question_id"); 44 String handle_name = rs.getString("hanle_name"); 45 String title = rs.getString("title"); 46 String contents = rs.getString("contents"); 47 int urgency = rs.getInt("urgency"); 48 String edit_delete_key = rs.getString("edit_delete_key"); 49 String regist_timestamp = rs.getString("regist_timestamp"); 50 String update_timestamp = rs.getString("update_timestamp"); 51 52 // 質問の情報を設定 53 Question question = new Question(question_id, handle_name, title, contents, urgency, edit_delete_key, regist_timestamp, update_timestamp); 54// List<Question> questionList = getQuestionListLogic.execute(); 55 questionList.add(question); 56} 57 } catch (SQLException e){ 58 e.printStackTrace(); 59// return null; 60 } 61// return questionList; 62return questionList; 63 } 64}
java
1package model; 2 3import java.util.List; 4 5import DAO.QuestionDAO; 6 7public class GetLogic { 8 public List<Question> execute(){ 9 QuestionDAO dao = new QuestionDAO(); 10 List<Question> questionList = dao.findAll(); 11 return questionList; 12 } 13} 14
----------追加----------------
いただいた回答より編集致しましたが
List<Question> list = (List<Question>) session.getAttribute("questionList");の行で以下エラーが出ます。ご確認いただけますでしょうか。
エラー:この行で見つかった複数の注釈: -Listを型に解決できません
jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" %> 3<%@ page import="model.Question"%> 4<% 5 @SuppressWarnings("unchecked") 6 List<Question> list = (List<Question>) session.getAttribute("questionList"); 7%> 8 9<!DOCTYPE html> 10<html lang="ja"> 11<head> 12 <meta charset="utf-8"> 13 <meta name="viewport" content=“width=device-width,initial-scale=1.0”> 14 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 15 <title>課題1</title> 16 <link rel="stylesheet" href="css/QandAList.css"> 17</head> 18<body> 19<header> 20 <div class="container"> 21 <p>質問一覧</p> 22 <a href="/QandASystem/RegistQuestionServlet">質問する</a> 23 </div> 24</header> 25<div class="main"> 26 <div class="container"> 27 <table border="1"> 28 <thead> 29 <tr> 30 <th class="no" width="7%">No</th> 31 <th width="15%">緊急度</th> 32 <th width="40%">タイトル</th> 33 <th>登録者</th> 34 <th>登録日</th> 35 <th>更新日</th> 36 </tr> 37 </thead> 38 <tbody> 39 <% for (int i=0; i<list.size(); i++) { %> 40 <tr> 41 <td><%=i+1%></td> 42 <td><%=list.get(i).getUrgency()%></td> 43 <td><%=list.get(i).getTitle()%></td> 44 <td><%=list.get(i).getHandle_name()%></td> 45 <td><%=list.get(i).getRegist_timestamp()%></td> 46 <td><%=list.get(i).getUpdate_timestamp()%></td> 47 </tr> 48 <tr> 49 <td><%=i+1%></td> 50 <td><%=list.get(i).getUrgency()%></td> 51 <td><%=list.get(i).getTitle()%></td> 52 <td><%=list.get(i).getHandle_name() %></td> 53 <td><%=list.get(i).getRegist_timestamp() %></td> 54 <td><%=list.get(i).getUpdate_timestamp() %></td> 55 </tr> 56 <tr> 57 <td><%=i+1%></td> 58 <td><%=list.get(i).getUrgency()%></td> 59 <td><%=list.get(i).getTitle()%></td> 60 <td><%=list.get(i).getHandle_name() %></td> 61 <td><%=list.get(i).getRegist_timestamp() %></td> 62 <td><%=list.get(i).getUpdate_timestamp() %></td> 63 </tr> 64 <tr> 65 <td><%=i+1%></td> 66 <td><%=list.get(i).getUrgency()%></td> 67 <td><%=list.get(i).getTitle()%></td> 68 <td><%=list.get(i).getHandle_name() %></td> 69 <td><%=list.get(i).getRegist_timestamp() %></td> 70 <td><%=list.get(i).getUpdate_timestamp() %></td> 71 </tr> 72 <tr> 73 <td>1</td> 74 <td><%=list.get(i).getUrgency()%></td> 75 <td><%=list.get(i).getTitle()%></td> 76 <td><%=list.get(i).getHandle_name() %></td> 77 <td><%=list.get(i).getRegist_timestamp() %></td> 78 <td><%=list.get(i).getUpdate_timestamp() %></td> 79 </tr> 80 <% } %> 81 </tbody> 82 </table> 83 </div> 84</div> 85<script src="js/main.js"></script> 86</body> 87</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/06 11:44
2020/04/06 14:24
2020/04/07 15:44
2020/04/08 03:53
2020/04/09 14:43
2020/04/09 16:23
2020/04/11 10:44
2020/04/11 11:05
2020/04/11 12:25
2020/04/11 13:18
2020/04/11 17:06
2020/04/12 05:16
2020/04/12 09:25
2020/04/12 10:54
2020/04/12 12:45