Javaアプリ (Q&Aシステム)で、すでに登録済みの質問を編集したい
※最初の質問時にあらかじめ設定した編集キーと入力した編集キーが一致した場合に編集可能にする
上記の [最初の質問投稿時に設定した編集キー] の取得ができず、入力キーと比較ができません。
取得方法をご教示いただきたいです。
質問編集画面へ遷移させるコントローラ
ConfirmServlet
package servlet;
import java.io.IOException;
import java.util.List;
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 javax.servlet.http.HttpSession;
import logic.ConfirmQuestionLogic;
import logic.GetAnswerLogic;
import model.Answer;
import model.Question;
/**
* Servlet implementation class ConfirmServlet
*/
@WebServlet("/ConfirmServlet")
public class ConfirmServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//文字コードエンコーディング
request.setCharacterEncoding("UTF-8");
//リクエストパラメータ取得
// int question_id = this.question_id;
int question_id = Integer.parseInt(request.getParameter("question_id"));
//選択された行の質問リスト取得
ConfirmQuestionLogic ConfirmQuestionLogic = new ConfirmQuestionLogic();
List<Question> questionList = ConfirmQuestionLogic.confirm(question_id);
HttpSession session = request.getSession();
session.setAttribute("questionList", questionList);
//選択された行の回答リスト取得
GetAnswerLogic GetAnswerLogic = new GetAnswerLogic();
List<Answer> answerList = GetAnswerLogic.getAnswer(question_id);
session.setAttribute("answerList", answerList);
//質問リスト画面へフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/jsp/QandAComfirm.jsp");
dispatcher.forward(request, response);
}
}
編集画面へ遷移し、入力された値と設定されているキーを比較して更新するコントローラ
EditQuestionServlet
package servlet;
import java.io.IOException;
import java.util.List;
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 javax.servlet.http.HttpSession;
import logic.EditQuestionLogic;
import logic.GetQuestionLogic;
import model.Question;
/**
* Servlet implementation class EditServlet
*/
@WebServlet("/EditQuestionServlet")
public class EditQuestionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private int question_id;
private String update_timestamp;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//リクエストから取得した情報を表示
HttpSession session = request.getSession();
Question questionList = (Question) request.getAttribute("questionList");
String eKey = questionList.getEdit_delete_key();
//文字コードエンコーディング
request.setCharacterEncoding("UTF-8");
//リクエストパラメータ取得
String editKey = request.getParameter("edit_delete_key");
if(eKey == editKey){
//入力されたキーの質問リストを取得してスコープへ保存
EditQuestionLogic EditQuestionLogic = new EditQuestionLogic();
List<Question> EditQuestionList = EditQuestionLogic.findKey(editKey);
session.setAttribute("EditQuestionList", EditQuestionList);
//質問編集画面へフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/jsp/QandAEdit.jsp");
dispatcher.forward(request, response);
} else {
//質問リスト画面へフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/jsp/QandAList.jsp");
dispatcher.forward(request, response);
}
}
}
QandAComfirm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<%@ page import="model.Question"%>
<%@ page import="model.Answer"%>
<%@ page import="java.util.List"%>
<%
@SuppressWarnings("unchecked")
List<Question> qlist = (List<Question>) session.getAttribute("questionList");
%>
<%
@SuppressWarnings("unchecked")
List<Answer> alist = (List<Answer>) session.getAttribute("answerList");
%>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>質問確認画面</title>
<link rel="stylesheet" href="css/QandAConfirm.css">
</head>
<body>
<header>
<div class="container">
<div class="header-flex">
<div class="header-left">
<h3>質問内容</h3>
</div>
<div class="header-right">
<a href="#" class="btn">戻る</a>
</div>
</div>
</div>
</header>
<div class="inform">
<div class="container">
<form name="questionForm" action="EditQuestionServlet" method="get">
<table border="1">
<% for (int i=0; i<qlist.size(); i++) { %>
<tr>
<th>名前(ハンドルネーム)</th>
<td><%=qlist.get(i).getHandle_name()%></td>
</tr>
<tr>
<th>タイトル</th>
<td><%=qlist.get(i).getTitle()%></td>
</tr>
<tr>
<th class="contents">内容</th>
<td><%=qlist.get(i).getContents()%></td>
</tr>
<tr>
<th>緊急度</th>
<td><%=qlist.get(i).getUrgency()%></td>
</tr>
<% } %>
</table>
</form>
</div>
</div>
<div class="key">
<div class="container">
<div class="key-flex">
<form name="keyForm" action="EditQuestionServlet" method="get">
<table border="1">
<tr>
<th>編集・削除キー</th>
<td><input type="text" name="edit_delete_key"></td>
</tr>
</table>
<div class="key-a">
<input type="submit" name="edit" value="編集する">
</div>
</form>
</div>
</div>
</div>
<div class="answer">
<div class="container">
<table border="1">
<% for (int i=0; i<alist.size(); i++) { %>
<tr>
<th>回答者<input type="text" class="table-answer"><%=alist.get(i).getHandle_name()%></th>
<th>回答日時<input type="text" class="table-answer"><%=alist.get(i).getRegist_timestamp()%></th>
</tr>
</table>
<table>
<tr>
<td>
<textarea name="answer" id="answer"><%=alist.get(i).getContents()%></textarea>
</td>
</tr>
</table>
<table border="1">
<tr>
<th>回答者<input type="text" class="table-answer"><%=alist.get(i).getHandle_name()%></th>
<th>回答日時<input type="text" class="table-answer"><%=alist.get(i).getRegist_timestamp()%></th>
</tr>
</table>
<table>
<tr>
<td>
<textarea name="answer" id="answer"><%=alist.get(i).getContents()%></textarea>
</td>
</tr>
<% } %>
</table>
</div>
</div>
<div class="kaitou">
<div class="container">
<p>回答する</p>
<table border="1">
<tr>
<th>名前(ハンドルネーム)</th>
<td><input type="text"></td>
</tr>
<tr>
<th>回答</th>
<td><textarea rows="10"></textarea></td>
</tr>
</table>
<div class="answer-btn">
<a href="#" class="btn" id="ans">回答する</a>
</div>
</div>
</div>
<script src="../WebContent/js/QandAConfirm.js"></script>
</body>
</html>
QandAEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<%@ page import="model.Question"%>
<%@ page import="java.util.List"%>
<%
@SuppressWarnings("unchecked")
List<Question> elist = (List<Question>) session.getAttribute("EditQuestionList");
%>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content=“width=device-width,initial-scale=1.0”>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>質問編集画面</title>
<link rel="stylesheet" href="css/QandAEdit.css">
</head>
<body>
<header>
<div class="container">
<div class="header-flex">
<div class="header-left">
<h3>質問編集</h3>
</div>
<div class="header-right">
<a href="#" class="btn">戻る</a>
</div>
</div>
</div>
</header>
<section class="regist-form">
<div class="container">
<form action="/QandASystem/EditQuestionServlet" method="post">
<table border="1">
<% for (int i=0; i<elist.size(); i++) { %>
<tr>
<th>名前(ハンドルネーム)</th>
<td><input type="text" value=<%=elist.get(i).getHandle_name()%> class="inp"></td>
</tr>
<tr>
<th>タイトル</th>
<td><input type="text" value=<%=elist.get(i).getTitle()%> class="inp"></td>
</tr>
<tr>
<th class="contents">内容</th>
<td><textarea rows="16"><%=elist.get(i).getContents()%></textarea></td>
</tr>
<tr class="urgency">
<th>緊急度</th>
<td>
<label><input type="radio" name="urgency" value="1">急いでいます</label>
<label><input type="radio" name="urgency" value="2">困ってます</label>
<label><input type="radio" name="urgency" value="3">いつでも</label>
</td>
</tr>
<tr>
<th>編集・削除キー</th>
<td><input type="text" value=<%=elist.get(i).getEdit_delete_key()%> class="inp"></td>
</tr>
<% } %>
</table>
<input type="submit" value="編集する">
</form>
</div>
</section>
<script src="js/QandAEdit.js"></script>
</body>
</html>
Question questionList = (Question) request.getAttribute("questionList");
から、おそらくEditQuestionServlet
はどこかからforwardされてくるのだと思いますが、そのときにquestionList
は設定されているでしょうか。