回答編集履歴

1

ソースの修正

2017/07/28 01:05

投稿

mr-hisa-child
mr-hisa-child

スコア294

test CHANGED
@@ -22,19 +22,161 @@
22
22
 
23
23
 
24
24
 
25
- ```java
26
-
27
- protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
25
+ formタグのmethod属性は、投稿情報の送信時に使用するので、上記の修正に合わせてください。
28
-
29
- // 画面表示処理
30
-
31
- }
32
26
 
33
27
 
34
28
 
35
- protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
29
+ BoardServlet.java
36
30
 
31
+ ```java
32
+
37
- // 投稿処理
33
+ @WebServlet("/board")
34
+
35
+ public class BoardServlet extends HttpServlet {
36
+
37
+
38
+
39
+ @Override
40
+
41
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42
+
43
+
44
+
45
+ String id = Optional.ofNullable(request.getParameter("id")).orElse("");
46
+
47
+ String title = Optional.ofNullable(request.getParameter("title")).orElse("");
48
+
49
+ String body = Optional.ofNullable(request.getParameter("body")).orElse("");
50
+
51
+
52
+
53
+ FileWriter fw = new FileWriter(request.getRealPath("/WEB-INF/classes/bbs_history.txt"),true);
54
+
55
+
56
+
57
+ try (BufferedWriter bw = new BufferedWriter(fw)) {
58
+
59
+ bw.write(id + "\r\n");
60
+
61
+ bw.write(title + "\r\n");
62
+
63
+ bw.write(body + "\r\n");
64
+
65
+ }
66
+
67
+
68
+
69
+ saveArticle(new Article(id,title,body));
70
+
71
+
72
+
73
+ response.sendRedirect("board");
74
+
75
+ }
76
+
77
+
78
+
79
+ @SuppressWarnings("unused")
80
+
81
+ @Override
82
+
83
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
84
+
85
+
86
+
87
+ response.setContentType("text/html;charset=UTF-8");
88
+
89
+ PrintWriter out = response.getWriter();
90
+
91
+
92
+
93
+ out.println("<html>");
94
+
95
+ out.println("<head>");
96
+
97
+ out.println("<title>BordServlet</title>");
98
+
99
+ out.println("</head>");
100
+
101
+ out.println("<body>");
102
+
103
+ out.println("<h1>掲示板</h1>");
104
+
105
+
106
+
107
+ out.println("<form action='board' method='POST'>");
108
+
109
+
110
+
111
+ out.println("<table>");
112
+
113
+ out.println("<tr><th>発信者ID</th><td><input type = text name='id'></td></tr>");
114
+
115
+ out.println("<tr><th>タイトル</th><td><input type = text name='title'></td></tr>");
116
+
117
+ out.println("<tr><th>本文</th><td><textarea name='body' rows=5></textarea></td></tr>");
118
+
119
+ out.println("</table>");
120
+
121
+ out.println("<br>");
122
+
123
+ out.println("<input type=submit value='投稿'>");
124
+
125
+ out.println("</form>");
126
+
127
+
128
+
129
+ List<Article> articles = getArticleList();
130
+
131
+
132
+
133
+ articles.stream().forEach(article -> {
134
+
135
+ out.println("<hr>");
136
+
137
+ out.println("発信者ID");
138
+
139
+ out.println("<p>" + article.getSenderID() + "</p>");
140
+
141
+ out.println("タイトル");
142
+
143
+ out.println("<p>" + article.getTitle() + "</p>");
144
+
145
+ out.println("本文");
146
+
147
+ out.println("<pre>" + article.getBody() + "</pre>");
148
+
149
+ out.println("<hr>");
150
+
151
+ });
152
+
153
+
154
+
155
+ out.println("</body>");
156
+
157
+ out.println("</html>");
158
+
159
+ }
160
+
161
+
162
+
163
+ private List<Article> getArticleList(){
164
+
165
+ return Optional.ofNullable((List<Article>)this.getServletContext().getAttribute("articles")).orElse(new ArrayList<>());
166
+
167
+ }
168
+
169
+
170
+
171
+ private void saveArticle(Article article){
172
+
173
+ List<Article> articles = getArticleList();
174
+
175
+ articles.add(article);
176
+
177
+ this.getServletContext().setAttribute("articles", articles);
178
+
179
+ }
38
180
 
39
181
  }
40
182
 
@@ -42,4 +184,8 @@
42
184
 
43
185
 
44
186
 
45
- formタグmethod属性は、投稿情報の送信時使用するので、上記の修正に合わせてください。
187
+ 上記ソースは、表示する投稿データをサーブレットコンテキスト保持しているので、
188
+
189
+ リロードを行うと投稿したデータが初期化されます。
190
+
191
+ 投稿データは、サーブレットコンテキストではなくファイルから読み込む必要があります。