質問編集履歴

3

login\.jspを削除、list\.jspを追加。

2016/09/19 06:28

投稿

yuichikubo
yuichikubo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -74,366 +74,312 @@
74
74
 
75
75
  import javax.servlet.http.HttpServletResponse;
76
76
 
77
- //Servletとして必要なインターフェース・クラスをインポート//
78
-
79
77
 
80
78
 
81
79
  @WebServlet(name = "MemoServlet", urlPatterns = { "/MemoServlet" })
82
80
 
83
81
  public class MemoServlet extends HttpServlet {
84
82
 
83
+
84
+
85
+ private static final long serialVersionUID = 1L;
86
+
85
87
 
86
88
 
89
+ private static final String JSP_BASE = "/WEB-INF/jsp/";
90
+
87
91
 
88
92
 
89
- private static final long serialVersionUID = 1L;
93
+ private Connection _pooledConnection;
94
+
95
+
96
+
97
+ public MemoServlet() {
98
+
99
+ _pooledConnection = null;
100
+
101
+ }
102
+
103
+
104
+
105
+ @Override
106
+
107
+ public void destroy() {
108
+
109
+ if (_pooledConnection != null) {
110
+
111
+ try {
112
+
113
+ _pooledConnection.close();
114
+
115
+ } catch (SQLException e) {
116
+
117
+ ;
118
+
119
+ }
120
+
121
+ _pooledConnection = null;
122
+
123
+ }
124
+
125
+
126
+
127
+ super.destroy();
128
+
129
+ }
130
+
131
+
132
+
133
+ @Override
134
+
135
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
136
+
137
+ throws ServletException, IOException {
138
+
139
+
140
+
141
+ String action = req.getParameter("action");
142
+
143
+
144
+
145
+ String forward = null;
146
+
147
+ if("login".equals(action)) {
148
+
149
+
150
+
151
+ forward = JSP_BASE + "login.jsp";
152
+
153
+ }else{
154
+
155
+
156
+
157
+ throw new ServletException("不正なリクエストです");
158
+
159
+ }
160
+
161
+
162
+
163
+ RequestDispatcher dispatcher = req.getRequestDispatcher(forward);
164
+
165
+ dispatcher.forward(req, resp);
166
+
167
+ }
168
+
169
+ @Override
170
+
171
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
172
+
173
+ throws ServletException, IOException {
174
+
175
+
176
+
177
+ String action = req.getParameter("action");
178
+
179
+
180
+
181
+ String forward = null;
182
+
183
+ if ("login_action".equals(action)) {
184
+
185
+
186
+
187
+ forward = doLoginAction(req, resp);
188
+
189
+ } else {
190
+
191
+
192
+
193
+ forward = doError(req, resp, "不正なリクエストです");
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+ RequestDispatcher dispatcher = req.getRequestDispatcher(forward);
202
+
203
+ dispatcher.forward(req, resp);
204
+
205
+ }
206
+
207
+
208
+
209
+
210
+
211
+ private String doLoginAction(HttpServletRequest req,
212
+
213
+ HttpServletResponse resp) throws ServletException, IOException {
214
+
215
+ String userID = req.getParameter("user_id");
216
+
217
+ String password = req.getParameter("password");
218
+
219
+ if (userID == null || password == null) {
220
+
221
+ throw new ServletException("不正なパラメータです。");
222
+
223
+ }
224
+
225
+
226
+
227
+ try{
228
+
229
+
230
+
231
+ User user = getUser(userID, password);
232
+
233
+ if(user == null) {
234
+
235
+ return doError(req, resp, "不正なユーザIDもしくはパラメータです。");
236
+
237
+ }
90
238
 
91
239
 
92
240
 
93
- /**
241
+
94
-
242
+
95
- * JSPのベースディレクトリ。
243
+ Memo[] memos = getMemos();
96
-
97
- */
98
-
99
- private static final String JSP_BASE = "/WEB-INF/jsp/";
100
244
 
101
245
 
102
246
 
103
- /**
247
+
104
-
105
- * データベースのコネクションを保持します。
248
+
106
-
107
- */
108
-
109
- private Connection _pooledConnection;
249
+ req.setAttribute("memos", memos);
110
-
111
-
112
250
 
113
251
 
114
252
 
115
- /**
116
-
117
- * 構築します。
118
-
119
- */
120
-
121
- public MemoServlet() {
253
+ return JSP_BASE + "list.jsp";
122
-
254
+
123
- _pooledConnection = null;
255
+ }catch(SQLException e) {
256
+
257
+ return doError(req, resp, e.getMessage());
258
+
259
+ }
124
260
 
125
261
  }
126
262
 
127
263
 
128
264
 
265
+
266
+
267
+ private String doError(HttpServletRequest req, HttpServletResponse resp,
268
+
269
+ String message) throws ServletException, IOException {
270
+
271
+ req.setAttribute("message", message);
272
+
273
+
274
+
275
+ return JSP_BASE + "error.jsp";
276
+
277
+ }
278
+
129
- @Override
279
+ private Connection getConnection()
130
-
280
+
131
- public void destroy() {
281
+ throws SQLException {
132
-
282
+
283
+
284
+
133
- if (_pooledConnection != null) {
285
+ if(_pooledConnection != null) {
286
+
134
-
287
+ return _pooledConnection;
288
+
289
+ }
290
+
135
- try {
291
+ try {
292
+
293
+
294
+
136
-
295
+ Class.forName( "com.mysql.jdbc.Driver" );
296
+
297
+
298
+
299
+
300
+
301
+ _pooledConnection = DriverManager.getConnection(
302
+
303
+ "jdbc:mysql://localhost/memo", "root", "ky890703");
304
+
305
+ return _pooledConnection;
306
+
307
+ } catch (ClassNotFoundException e) {
308
+
137
- _pooledConnection.close();
309
+ _pooledConnection = null;
310
+
138
-
311
+ throw new SQLException(e);
312
+
139
- } catch (SQLException e) {
313
+ } catch (SQLException e) {
314
+
140
-
315
+ _pooledConnection = null;
316
+
317
+ throw e;
318
+
141
- ;
319
+ }
320
+
321
+ }
322
+
323
+
324
+
325
+ private User getUser(String userID, String password)
326
+
327
+ throws SQLException {
328
+
329
+ Statement statement = null;
330
+
331
+ try {
332
+
333
+
334
+
335
+ statement = getConnection().createStatement();
336
+
337
+ ResultSet resultSet = statement
338
+
339
+ .executeQuery("SELECT USER_ID,USER_NAME FROM USER WHERE USER_ID='"
340
+
341
+ + userID + "' AND PASSWORD='" + password + "'");
342
+
343
+ boolean br = resultSet.first();
344
+
345
+ if (br == false) {
346
+
347
+ return null;
142
348
 
143
349
  }
144
350
 
351
+ User user = new User();
352
+
353
+ user.setId(resultSet.getString("USER_ID"));
354
+
355
+ user.setName(resultSet.getString("USER_NAME"));
356
+
357
+
358
+
359
+ return user;
360
+
361
+ }catch(SQLException e) {
362
+
145
363
  _pooledConnection = null;
146
364
 
365
+ throw e;
366
+
367
+ } finally {
368
+
369
+ if (statement != null) {
370
+
371
+ statement.close();
372
+
373
+ statement = null;
374
+
147
- }
375
+ }
148
-
149
-
150
-
376
+
151
- super.destroy();
377
+ }
152
378
 
153
379
  }
154
380
 
155
381
 
156
382
 
157
- @Override
158
-
159
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
160
-
161
- throws ServletException, IOException {
162
-
163
- // 要求からactionパラメータを取得
164
-
165
- String action = req.getParameter("action");
166
-
167
-
168
-
169
- String forward = null;
170
-
171
- if("login".equals(action)) {
172
-
173
- // ログイン画面の処理
174
-
175
- // login.jspへフォワードする
176
-
177
- forward = JSP_BASE + "login.jsp";
178
-
179
- }else{
180
-
181
- // 不正なアクションの場合
182
-
183
- throw new ServletException("不正なリクエストです");
184
-
185
- }
186
-
187
-
188
-
189
- // JSPへのフォワード
190
-
191
- RequestDispatcher dispatcher = req.getRequestDispatcher(forward);
192
-
193
- dispatcher.forward(req, resp);
194
-
195
- }
196
-
197
- @Override
198
-
199
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
200
-
201
- throws ServletException, IOException {
202
-
203
- // 要求からactionパラメータを取得
204
-
205
- String action = req.getParameter("action");
206
-
207
-
208
-
209
- String forward = null;
210
-
211
- if ("login_action".equals(action)) {
212
-
213
- // ログイン画面からの入力受付
214
-
215
- forward = doLoginAction(req, resp);
216
-
217
- } else {
218
-
219
- // 不正なアクションの場合
220
-
221
- forward = doError(req, resp, "不正なリクエストです");
222
-
223
- }
224
-
225
-
226
-
227
- // JSPへのフォワード
228
-
229
- RequestDispatcher dispatcher = req.getRequestDispatcher(forward);
230
-
231
- dispatcher.forward(req, resp);
232
-
233
- }
234
-
235
-
236
-
237
- /**
238
-
239
- * ログイン処理をおこないます。
240
-
241
- */
242
-
243
- private String doLoginAction(HttpServletRequest req,
244
-
245
- HttpServletResponse resp) throws ServletException, IOException {
246
-
247
- String userID = req.getParameter("user_id");
248
-
249
- String password = req.getParameter("password");
250
-
251
- if (userID == null || password == null) {
252
-
253
- throw new ServletException("不正なパラメータです。");
254
-
255
- }
256
-
257
-
258
-
259
- try{
260
-
261
- // ユーザを取得する
262
-
263
- User user = getUser(userID, password);
264
-
265
- if(user == null) {
266
-
267
- return doError(req, resp, "不正なユーザIDもしくはパラメータです。");
268
-
269
- }
270
-
271
-
272
-
273
- // メモを取得する
274
-
275
- Memo[] memos = getMemos();
276
-
277
-
278
-
279
- // アイテムを要求オブジェクトに格納する
280
-
281
- req.setAttribute("memos", memos);
282
-
283
-
284
-
285
- // 一覧を表示する
286
-
287
- return JSP_BASE + "list.jsp";
288
-
289
- }catch(SQLException e) {
290
-
291
- return doError(req, resp, e.getMessage());
292
-
293
- }
294
-
295
- }
296
-
297
-
298
-
299
- /**
300
-
301
- * エラーを表示します。
302
-
303
- */
304
-
305
-
306
-
307
- private String doError(HttpServletRequest req, HttpServletResponse resp,
308
-
309
- String message) throws ServletException, IOException {
310
-
311
- req.setAttribute("message", message);
312
-
313
-
314
-
315
- // エラーを表示する
316
-
317
- return JSP_BASE + "error.jsp";
318
-
319
- }
320
-
321
- /**
322
-
323
- * 接続オブジェクトを生成します。
324
-
325
- */
326
-
327
- private Connection getConnection()
328
-
329
- throws SQLException {
330
-
331
- // Connectionの準備
332
-
333
- if(_pooledConnection != null) {
334
-
335
- return _pooledConnection;
336
-
337
- }
338
-
339
- try {
340
-
341
-
342
-
343
- Class.forName( "com.mysql.jdbc.Driver" );
344
-
345
-
346
-
347
-
348
-
349
- _pooledConnection = DriverManager.getConnection(
350
-
351
- "jdbc:mysql://localhost/memo", "root", "ky890703");
352
-
353
- return _pooledConnection;
354
-
355
- } catch (ClassNotFoundException e) {
356
-
357
- _pooledConnection = null;
358
-
359
- throw new SQLException(e);
360
-
361
- } catch (SQLException e) {
362
-
363
- _pooledConnection = null;
364
-
365
- throw e;
366
-
367
- }
368
-
369
- }
370
-
371
-
372
-
373
- /**
374
-
375
- * ユーザを取得します。
376
-
377
- */
378
-
379
- private User getUser(String userID, String password)
380
-
381
- throws SQLException {
382
-
383
- Statement statement = null;
384
-
385
- try {
386
-
387
- // SQL文を発行
388
-
389
- statement = getConnection().createStatement();
390
-
391
- ResultSet resultSet = statement
392
-
393
- .executeQuery("SELECT USER_ID,USER_NAME FROM USER WHERE USER_ID='"
394
-
395
- + userID + "' AND PASSWORD='" + password + "'");
396
-
397
- boolean br = resultSet.first();
398
-
399
- if (br == false) {
400
-
401
- return null;
402
-
403
- }
404
-
405
- User user = new User();
406
-
407
- user.setId(resultSet.getString("USER_ID"));
408
-
409
- user.setName(resultSet.getString("USER_NAME"));
410
-
411
-
412
-
413
- return user;
414
-
415
- }catch(SQLException e) {
416
-
417
- _pooledConnection = null;
418
-
419
- throw e;
420
-
421
- } finally {
422
-
423
- if (statement != null) {
424
-
425
- statement.close();
426
-
427
- statement = null;
428
-
429
- }
430
-
431
- }
432
-
433
- }
434
-
435
-
436
-
437
383
  一部省略
438
384
 
439
385
  }
@@ -444,101 +390,223 @@
444
390
 
445
391
  ```html
446
392
 
447
- ★login.jsp
393
+ ★list.jsp
448
394
 
449
395
 
450
396
 
451
397
  <%@ page language="java" contentType="text/html; charset=utf-8" %>
452
398
 
399
+ <%@ page import="pack.User" %>
400
+
401
+ <%@ page import="pack.Memo" %>
402
+
403
+ <%@ page import="java.util.Calendar" %>
404
+
405
+ <%
406
+
407
+ User currentUser = (User) request.getSession().getAttribute("currentUser");
408
+
409
+ %>
410
+
453
411
  <html>
454
412
 
455
413
  <head>
456
414
 
457
415
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
458
416
 
459
- <title>ログイン</title>
417
+ <title>検索一覧</title>
460
418
 
461
419
  <link rel="STYLESHEET" href="todo.css" type="text/css">
462
420
 
463
- <script type="text/javascript" src="login.js">
464
-
465
- </script>
466
-
467
421
  </head>
468
422
 
423
+ <body>
424
+
425
+ <!-- 作業登録・検索 -->
426
+
469
- <body onload="fieldChanged();">
427
+ <table border="0" width="90%" class="toolbar">
470
-
428
+
471
- <h1>ログイン</h1>
429
+ <tr>
472
-
473
-
474
-
475
- <div align="center">
476
-
477
- <table border="0">
478
430
 
479
431
  <form action="todo" method="post">
480
432
 
481
- <input type="hidden" name="action" value="login_action">
433
+ <input type="hidden" name="action" value="add">
482
-
434
+
483
- <tr>
435
+ <td>
484
-
485
- <th class="login_field">
436
+
486
-
487
- ID
488
-
489
- </th>
490
-
491
- <td class="login_field">
492
-
493
- <input type="text" name="user_id" value=""
437
+ <input type="submit" value="作業登録">
494
-
495
- size="24" id="user_id"
438
+
496
-
497
- onkeyup="fieldChanged();"
498
-
499
- onchange="fieldChanged();">
500
-
501
- </td>
439
+ </td>
502
-
503
- </tr>
504
-
505
- <tr>
506
-
507
- <th class="login_field">
508
-
509
- PW
510
-
511
- </th>
512
-
513
- <td class="login_field">
514
-
515
- <input type="password" name="password" value=""
516
-
517
- size="24" id="password"
518
-
519
- onkeyup="fieldChanged();"
520
-
521
- onchange="fieldChanged();">
522
-
523
- </td>
524
-
525
- </tr>
526
-
527
- <tr>
528
-
529
- <td colspan="2" class="login_button">
530
-
531
- <input type="submit" value="ログイン" id="login">
532
-
533
- </td>
534
-
535
- </tr>
536
440
 
537
441
  </form>
538
442
 
443
+ <td align="left">
444
+
445
+ <table border="0">
446
+
447
+ <tr>
448
+
449
+ <td>
450
+
451
+ タイトル
452
+
453
+ </td>
454
+
455
+ <form action="todo" method="post">
456
+
457
+ <input type="hidden" name="action" value="search">
458
+
459
+ <td>
460
+
461
+ <input type="text" name="title" value="" size="24">
462
+
463
+ </td>
464
+
465
+ </form>
466
+
467
+ </tr>
468
+
469
+ <tr>
470
+
471
+ <td>
472
+
473
+ 更新日
474
+
475
+ </td>
476
+
477
+ <form action="todo" method="post">
478
+
479
+ <input type="hidden" name="action" value="search">
480
+
481
+ <td>
482
+
483
+ <input type="text" name="update_datetime" value="" size="24">
484
+
485
+ </td>
486
+
487
+ </form>
488
+
489
+ </tr>
490
+
491
+ <tr>
492
+
493
+ <td>
494
+
495
+ <input type="submit" value="検索">
496
+
497
+ </td>
498
+
499
+ </tr>
500
+
539
- </table>
501
+ </table>
540
-
502
+
541
- </div>
503
+ </td>
504
+
505
+ </tr>
506
+
507
+ </table>
508
+
509
+ <%
510
+
511
+ Memo[] memos = (Memo[]) request.getAttribute("items");
512
+
513
+ if(memos.length == 0) {
514
+
515
+ // アイテムが存在しない場合
516
+
517
+ %>
518
+
519
+ <div align="center">検索項目はありません。</div>
520
+
521
+ <%
522
+
523
+ }else{
524
+
525
+ %>
526
+
527
+ <table border="0" width="90%" class="list">
528
+
529
+ <tr>
530
+
531
+ <th>
532
+
533
+ ID
534
+
535
+ </th>
536
+
537
+ <th>
538
+
539
+ タイトル
540
+
541
+ </th>
542
+
543
+ <th>
544
+
545
+ 登録日
546
+
547
+ </th>
548
+
549
+ <th>
550
+
551
+ 更新日
552
+
553
+ </th>
554
+
555
+ </tr>
556
+
557
+ 一部省略
558
+
559
+ // メモを出力
560
+
561
+ for(int i = 0; i < memos.length; i ++) {
562
+
563
+ Memo memo = memos[i];
564
+
565
+ String styleAttr = "";
566
+
567
+ %>
568
+
569
+ <tr>
570
+
571
+ <td <%= styleAttr %>>
572
+
573
+ <%= memo.getId() %>
574
+
575
+ </td>
576
+
577
+ <td <%= styleAttr %>>
578
+
579
+ <%= memo.getTitle() %>
580
+
581
+ </td>
582
+
583
+ <td <%= styleAttr %>>
584
+
585
+ <%= memo.getInsertDate() %>
586
+
587
+ </td>
588
+
589
+ <td <%= styleAttr %>>
590
+
591
+ <%= memo.getUpdateDate() %>
592
+
593
+ </td>
594
+
595
+ </tr>
596
+
597
+ <%
598
+
599
+ }
600
+
601
+ %>
602
+
603
+ </table>
604
+
605
+ <%
606
+
607
+ }
608
+
609
+ %>
542
610
 
543
611
  </body>
544
612
 

2

閉じタグを追加。

2016/09/19 06:28

投稿

yuichikubo
yuichikubo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
 
38
38
 
39
- ```Java
39
+ ```java
40
40
 
41
41
  ★MemoServlet.java
42
42
 
@@ -438,9 +438,11 @@
438
438
 
439
439
  }
440
440
 
441
-
441
+ ```
442
+
443
+
444
+
442
-
445
+ ```html
443
-
444
446
 
445
447
  ★login.jsp
446
448
 
@@ -541,3 +543,5 @@
541
543
  </body>
542
544
 
543
545
  </html>
546
+
547
+ ```

1

「```ここに言語を入力 」を「```Java」に修正。

2016/09/19 03:10

投稿

yuichikubo
yuichikubo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
 
38
38
 
39
- ```ここに言語を入力
39
+ ```Java
40
40
 
41
41
  ★MemoServlet.java
42
42