質問編集履歴

1

ソースの追加・誤字修正

2017/05/18 06:27

投稿

shafi_bo
shafi_bo

スコア20

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,384 @@
16
16
 
17
17
 
18
18
 
19
+ ### ソースコード
20
+
21
+ JSP
22
+
23
+ ```
24
+
25
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
26
+
27
+ pageEncoding="UTF-8"%>
28
+
29
+ <%@ page import="javax.servlet.ServletContext"%>
30
+
31
+ <%@ page import="category.CategoryList"%>
32
+
33
+ <%@ page import="category.Category"%>
34
+
35
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
36
+
37
+ <html>
38
+
39
+ <head>
40
+
41
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
42
+
43
+ <title>Insert title here</title>
44
+
45
+ </head>
46
+
47
+ <body>
48
+
49
+ <div align="left">
50
+
51
+ <h1>ブログ作成</h1>
52
+
53
+ </div>
54
+
55
+ <hr>
56
+
57
+ <div align="center">
58
+
59
+
60
+
61
+ <form action="Master" method="post">
62
+
63
+
64
+
65
+ <div align="left">
66
+
67
+ <input type="text" placeholder="タイトル" name="title" size="80">
68
+
69
+
70
+
71
+ <p />
72
+
73
+ <%
74
+
75
+ ServletContext context = this.getServletContext();
76
+
77
+ CategoryList categoryList = (CategoryList) context.getAttribute("CategoryList");
78
+
79
+ %>
80
+
81
+ <div align="right">
82
+
83
+ カテゴリー: <select name="category">
84
+
85
+ <%
86
+
87
+ for (Category category : categoryList.getList()) {
88
+
89
+ %>
90
+
91
+ <option value=<%=category%>><%=category.getCategoryName()%></option>
92
+
93
+ <%
94
+
95
+ }
96
+
97
+ %>
98
+
99
+ </select>
100
+
101
+ </div>
102
+
103
+
104
+
105
+ <textarea name="body" cols="60" rows="20"></textarea>
106
+
107
+ </div>
108
+
109
+ <div align="right">
110
+
111
+ <button type="submit" name="Action" value="create">作成</button>
112
+
113
+ </div>
114
+
115
+ </form>
116
+
117
+
118
+
119
+ </div>
120
+
121
+ </body>
122
+
123
+ </html>
124
+
125
+ ```
126
+
127
+ JavaServlet
128
+
129
+ ```
130
+
131
+ package controller;
132
+
133
+
134
+
135
+ import java.io.IOException;
136
+
137
+
138
+
139
+ import javax.servlet.RequestDispatcher;
140
+
141
+ import javax.servlet.ServletException;
142
+
143
+ import javax.servlet.annotation.WebServlet;
144
+
145
+ import javax.servlet.http.HttpServlet;
146
+
147
+ import javax.servlet.http.HttpServletRequest;
148
+
149
+ import javax.servlet.http.HttpServletResponse;
150
+
151
+ import javax.servlet.http.HttpSession;
152
+
153
+
154
+
155
+ import model.User;
156
+
157
+
158
+
159
+ /**
160
+
161
+ * Servlet implementation class MasterController
162
+
163
+ */
164
+
165
+ @WebServlet("/Master")
166
+
167
+ public class MasterController extends HttpServlet {
168
+
169
+ private static final long serialVersionUID = 1L;
170
+
171
+
172
+
173
+ /**
174
+
175
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
176
+
177
+ */
178
+
179
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
180
+
181
+ throws ServletException, IOException {
182
+
183
+
184
+
185
+ // 直接起動防止
186
+
187
+ RequestDispatcher rd = request.getRequestDispatcher("top.jsp");
188
+
189
+ rd.forward(request, response);
190
+
191
+
192
+
193
+ }
194
+
195
+
196
+
197
+ /**
198
+
199
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
200
+
201
+ */
202
+
203
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
204
+
205
+ throws ServletException, IOException {
206
+
207
+
208
+
209
+ // エンコード
210
+
211
+ request.setCharacterEncoding("UTF-8");
212
+
213
+ response.setCharacterEncoding("UTF-8");
214
+
215
+
216
+
217
+ // 処理行動種別
218
+
219
+ String action = null;
220
+
221
+
222
+
223
+ // セッション取得
224
+
225
+ HttpSession session = request.getSession();
226
+
227
+
228
+
229
+ // 事前ログインユーザ定義
230
+
231
+ if (session.getAttribute("User") == null) {
232
+
233
+ User user = new User("admin", "suzukitarou", "suzukin");
234
+
235
+ session.setAttribute("User", user);
236
+
237
+ }
238
+
239
+
240
+
241
+ action = request.getParameter("Action"); // form送信
242
+
243
+ String url = null;
244
+
245
+
246
+
247
+ if (action == null) {
248
+
249
+ action = "null";
250
+
251
+ }
252
+
253
+
254
+
255
+ // 行動別処理
256
+
257
+ switch (action) {
258
+
259
+
260
+
261
+ /*執筆系*/
262
+
263
+
264
+
265
+ case "write":
266
+
267
+
268
+
269
+ if (session.getAttribute("User") != null) {
270
+
271
+ url = "write.jsp";
272
+
273
+ } else {
274
+
275
+ url = "login.jsp";
276
+
277
+ }
278
+
279
+ break;
280
+
281
+
282
+
283
+ // case "login": // ログイン処理
284
+
285
+ // url = "Login"; // ログインコントローラ
286
+
287
+ // String userId = request.getParameter("userid"); // ID取得
288
+
289
+ // String password = request.getParameter("password"); // パスワード取得
290
+
291
+ // request.setAttribute("USERID", userId); // ID送信
292
+
293
+ // request.setAttribute("PASSWORD", password); // パスワード送信
294
+
295
+ // request.setAttribute("ACTION","login"); // 行動送信
296
+
297
+ // break;
298
+
299
+
300
+
301
+ // case "agree": // ログイン完了処理
302
+
303
+ // url = "successlogin.jsp";
304
+
305
+ // break;
306
+
307
+
308
+
309
+ // case "incongruous": // ログイン失敗処理
310
+
311
+
312
+
313
+ // request.setAttribute("REASON", "incongruous");
314
+
315
+ //
316
+
317
+ // url = "failurelogin.jsp";
318
+
319
+ // break;
320
+
321
+
322
+
323
+ // case "prev": // 戻る
324
+
325
+ // url = "top.jsp";
326
+
327
+ // break;
328
+
329
+ // case "goLogin": // ログイン画面に移動
330
+
331
+ // url = "login.jsp";
332
+
333
+ // break;
334
+
335
+
336
+
337
+ case "create":
338
+
339
+ // タイトル取得
340
+
341
+ String title = request.getParameter("title");
342
+
343
+ // 本文取得
344
+
345
+ String body = request.getParameter("body");
346
+
347
+ // カテゴリー取得
348
+
349
+ // Category category = request.getParameterValues("Category");
350
+
351
+ //Category category = (Category)request.getAttribute("category");
352
+
353
+
354
+
355
+ // データの送信準備
356
+
357
+ request.setAttribute("Title", title);
358
+
359
+ request.setAttribute("Body", body);
360
+
361
+ request.setAttribute("Category", category);
362
+
363
+ url = "valuetest.jsp";
364
+
365
+ break;
366
+
367
+
368
+
369
+ default: // 予想とした処理と違うものが入ってきた場合
370
+
371
+ url = "top.jsp";
372
+
373
+ break;
374
+
375
+ }
376
+
377
+
378
+
379
+ // 画面遷移
380
+
381
+ RequestDispatcher rd = request.getRequestDispatcher(url);
382
+
383
+ rd.forward(request, response);
384
+
385
+
386
+
387
+ }
388
+
389
+
390
+
391
+ }
392
+
393
+ ```
394
+
395
+
396
+
19
397
 
20
398
 
21
399
  ###試したこと
@@ -24,7 +402,7 @@
24
402
 
25
403
  getParameter()では文字列型しか取れず、
26
404
 
27
- getAttribute()ではsubmit送信ではれなかった。
405
+ getAttribute()ではsubmit送信ではれなかった。
28
406
 
29
407
 
30
408