teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

ソースの修正

2017/07/28 01:05

投稿

mr-hisa-child
mr-hisa-child

スコア294

answer CHANGED
@@ -10,14 +10,87 @@
10
10
 
11
11
  入力データを送信するときはPOST(doPost)、画面を表示するときはGET(doGet)で行うようにしてください。
12
12
 
13
+ formタグのmethod属性は、投稿情報の送信時に使用するので、上記の修正に合わせてください。
14
+
15
+ BoardServlet.java
13
16
  ```java
14
- protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
15
- // 画面表示処理
17
+ @WebServlet("/board")
16
- }
18
+ public class BoardServlet extends HttpServlet {
17
19
 
20
+ @Override
18
- protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
21
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22
+
23
+ String id = Optional.ofNullable(request.getParameter("id")).orElse("");
24
+ String title = Optional.ofNullable(request.getParameter("title")).orElse("");
25
+ String body = Optional.ofNullable(request.getParameter("body")).orElse("");
26
+
27
+ FileWriter fw = new FileWriter(request.getRealPath("/WEB-INF/classes/bbs_history.txt"),true);
28
+
29
+ try (BufferedWriter bw = new BufferedWriter(fw)) {
30
+ bw.write(id + "\r\n");
31
+ bw.write(title + "\r\n");
32
+ bw.write(body + "\r\n");
33
+ }
34
+
35
+ saveArticle(new Article(id,title,body));
36
+
37
+ response.sendRedirect("board");
38
+ }
39
+
40
+ @SuppressWarnings("unused")
41
+ @Override
42
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43
+
44
+ response.setContentType("text/html;charset=UTF-8");
45
+ PrintWriter out = response.getWriter();
46
+
47
+ out.println("<html>");
48
+ out.println("<head>");
49
+ out.println("<title>BordServlet</title>");
50
+ out.println("</head>");
51
+ out.println("<body>");
52
+ out.println("<h1>掲示板</h1>");
53
+
54
+ out.println("<form action='board' method='POST'>");
55
+
56
+ out.println("<table>");
57
+ out.println("<tr><th>発信者ID</th><td><input type = text name='id'></td></tr>");
58
+ out.println("<tr><th>タイトル</th><td><input type = text name='title'></td></tr>");
59
+ out.println("<tr><th>本文</th><td><textarea name='body' rows=5></textarea></td></tr>");
60
+ out.println("</table>");
61
+ out.println("<br>");
62
+ out.println("<input type=submit value='投稿'>");
63
+ out.println("</form>");
64
+
65
+ List<Article> articles = getArticleList();
66
+
67
+ articles.stream().forEach(article -> {
68
+ out.println("<hr>");
69
+ out.println("発信者ID");
70
+ out.println("<p>" + article.getSenderID() + "</p>");
71
+ out.println("タイトル");
72
+ out.println("<p>" + article.getTitle() + "</p>");
73
+ out.println("本文");
74
+ out.println("<pre>" + article.getBody() + "</pre>");
75
+ out.println("<hr>");
76
+ });
77
+
78
+ out.println("</body>");
79
+ out.println("</html>");
80
+ }
81
+
82
+ private List<Article> getArticleList(){
83
+ return Optional.ofNullable((List<Article>)this.getServletContext().getAttribute("articles")).orElse(new ArrayList<>());
84
+ }
85
+
86
+ private void saveArticle(Article article){
87
+ List<Article> articles = getArticleList();
19
- // 投稿処理
88
+ articles.add(article);
89
+ this.getServletContext().setAttribute("articles", articles);
90
+ }
20
91
  }
21
92
  ```
22
93
 
23
- formタグmethod属性は、投稿情報の送信時使用するので、上記の修正に合わせてください。
94
+ 上記ソースは、表示する投稿データをサーブレットコンテキスト保持しているので、
95
+ リロードを行うと投稿したデータが初期化されます。
96
+ 投稿データは、サーブレットコンテキストではなくファイルから読み込む必要があります。