質問編集履歴

2

変更

2016/06/20 02:13

投稿

l_l_l_l_l_l_l_l
l_l_l_l_l_l_l_l

スコア38

test CHANGED
@@ -1 +1 @@
1
- henkou java.lang.NullPointerExceptio
1
+ java.lang.NullPointerExceptio
test CHANGED
File without changes

1

変更

2016/06/20 02:12

投稿

l_l_l_l_l_l_l_l
l_l_l_l_l_l_l_l

スコア38

test CHANGED
@@ -1 +1 @@
1
- JSP画面でエラーメッセージが表示されない
1
+ henkou java.lang.NullPointerExceptio
test CHANGED
@@ -1,12 +1,8 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- ユーザの新規登録の際、入力欄に一つでも未入力の箇所ある場合
4
-
5
- エラーメッセージを表示させたいのですされせん。
3
+ エラーが出てし
4
+
6
-
5
+ 重大: Servlet.service() for servlet [servlet.book_lend] in context with path [/Library] threw exception [java.lang.NullPointerException] with root cause
7
-
8
-
9
-
10
6
 
11
7
  ###JSP
12
8
 
@@ -14,7 +10,7 @@
14
10
 
15
11
  <%@ page contentType="text/html; charset=UTF-8"
16
12
 
17
- import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*"%>
13
+ import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*,java.util.Date,java.util.Calendar"%>
18
14
 
19
15
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
20
16
 
@@ -22,69 +18,289 @@
22
18
 
23
19
 
24
20
 
21
+ <html>
22
+
23
+
24
+
25
25
  <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
26
26
 
27
27
  url="jdbc:mysql://localhost/Library" user="root" password="" />
28
28
 
29
+ <sql:query var="rs" dataSource="${db}">
30
+
31
+ SELECT book.book_no,book.book_name,book.state,genre.genre_no,genre.genre_name,user.user_no,user.user_id,
32
+
33
+ lend.lend_date,lend.return_date FROM book LEFT JOIN lend ON book.book_no = lend.book_no LEFT JOIN
34
+
35
+ user ON user.user_no = lend.user_no LEFT JOIN genre ON book.genre_no = genre.genre_no
36
+
37
+
38
+
39
+ <c:if test="${!empty param['genreNo']}">
40
+
41
+ where book.genre_no = ?
42
+
43
+ <sql:param value="${param['genreNo']}" />
44
+
45
+ ORDER BY book.book_no asc;
46
+
29
- <html>
47
+ </c:if>
48
+
49
+
50
+
51
+ </sql:query>
52
+
53
+ <sql:query var="rs1" dataSource="${db}">
54
+
55
+ select * from genre
56
+
57
+ </sql:query>
58
+
59
+
60
+
61
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
62
+
63
+
30
64
 
31
65
  <head>
32
66
 
33
- <link rel="stylesheet" type="text/css" href="css/Library.css">
34
-
35
67
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
36
68
 
37
-
69
+ <link rel="stylesheet" type="text/css" href="css/Lending.css">
38
-
70
+
39
- <title>新規ユーザ登録画面</title>
71
+ <title>書籍貸出画面</title>
40
72
 
41
73
  </head>
42
74
 
43
75
  <body>
44
76
 
45
- <h2>新規ユーザ登録画面</h2>
46
-
47
-
48
-
49
- <form method="POST" action="newuser_tsuika">
50
-
51
- <h3>新規ユーザ追加</h3>
52
-
53
- 名前(姓) <input type="text" name="first_name" size="30" >
54
-
55
- 名前(名) <input type="text" name="last_name" size="30"> <br>
56
-
57
- カナ(姓) <input type="text" name="first_name_kana" size="30">
58
-
59
- カナ(名) <input type="text" name="last_name_kana" size="30"> <br>
60
-
61
- ID <input type="text" name="user_id" size="30">
62
-
63
- パスワード <input type="password" name="password" size="30"> <br> <br>
64
-
65
- <!-- <input name="admin" type="radio" value="0"></input> --> <input
66
-
67
- style="color: white; background-color: D57200;" type="submit"
68
-
69
- value="登録" />
70
-
71
-
77
+ <h2>書籍貸出画面</h2>
78
+
79
+
80
+
81
+ <br>
82
+
83
+ <form method="POST" action="book_lend.jsp">
84
+
85
+ <p>
86
+
87
+ ジャンル
88
+
89
+ <select name="genreNo">
90
+
91
+ <option value="">全て</option>
92
+
93
+ <c:forEach var="row" items="${rs1.rows}">
94
+
95
+ <option value=${row.genre_no}>${row.genre_name}</option>
96
+
97
+ </c:forEach>
98
+
99
+ </select>
100
+
101
+ </p>
102
+
103
+
104
+
105
+ <INPUT style="color:white;
106
+
107
+ background-color:D57200;" type="submit" value="絞り込む" />
108
+
109
+ </form>
110
+
111
+ <br>
112
+
113
+
114
+
115
+ <table border="1">
116
+
117
+ <tr>
118
+
119
+ <th>ID</th>
120
+
121
+ <th>書籍名</th>
122
+
123
+ <th>ジャンル</th>
124
+
125
+ <th>貸出日</th>
126
+
127
+ <th>返却予定日</th>
128
+
129
+ <th>状態</th>
130
+
131
+ <th>貸出/返却</th>
132
+
133
+ <th>貸りた人</th>
134
+
135
+ </tr>
136
+
137
+
138
+
139
+ <c:forEach var="row" items="${rs.rows}">
140
+
141
+ <tr>
142
+
143
+ <td>${row.book_no}</td>
144
+
145
+ <td>${row.book_name}</td>
146
+
147
+ <td>${row.genre_name}</td>
148
+
149
+
150
+
151
+ <td>
152
+
153
+ <c:if test="${empty row.lend_date}">-</c:if>
154
+
155
+ ${row.lend_date }
156
+
157
+ </td>
158
+
159
+
160
+
161
+ <td>
162
+
163
+ <c:if test="${empty row.lend_date}">-</c:if>
164
+
165
+ ${row.return_date }
166
+
167
+ </td>
168
+
169
+ <td>
170
+
171
+
172
+
173
+ <c:if test="${row.state == 2}" >
174
+
175
+ <span class="fontRed">貸出不可</span>
176
+
177
+ </c:if>
178
+
179
+ <c:if test="${row.state == 1 }">
180
+
181
+ <c:if test="${empty row.lend_date}">
182
+
183
+ 貸出可能
184
+
185
+ </c:if>
186
+
187
+ <c:if test="${!empty row.lend_date}">
188
+
189
+ <c:set var= "today" value="<%=new Date()%>"/>
190
+
191
+ <c:if test="${today > row.return_date}">
192
+
193
+ 返却遅延
194
+
195
+ </c:if>
196
+
197
+ <c:if test="${today <= row.return_date}">
198
+
199
+ 貸出中
200
+
201
+ </c:if>
202
+
203
+ </c:if>
204
+
205
+ </c:if>
206
+
207
+
208
+
209
+ </td>
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+ <td>
218
+
219
+ <c:if test="${empty row.lend_date}">
220
+
221
+ <c:if test="${row.state == 2}">-</c:if>
222
+
223
+ <c:if test="${row.state == 1}">
224
+
225
+ <form method="POST" action="book_lend">
226
+
227
+
228
+
229
+ <input type="hidden" value="${row.book_no}" name="book_no">
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+ <input type="hidden" value="${row.genre_no}" name="genre_no">
238
+
239
+
240
+
241
+
242
+
243
+ <INPUT style="color:white;
244
+
245
+ background-color:D57200; width:60px; height:40px;" type="submit" value="貸出" />
246
+
247
+ </form>
248
+
249
+ </c:if>
250
+
251
+ </c:if>
252
+
253
+
254
+
255
+
256
+
257
+ <c:if test="${!empty row.lend_date}">
258
+
259
+ <form method="GET" action="book_lend">
260
+
261
+ <input type="hidden" value=${row.book_no} name="book_no" />
262
+
263
+ <INPUT style="color:white;
264
+
265
+ background-color:AFD170; width:60px; height:40px;" type="submit" value="返却" />
72
266
 
73
267
  </form>
74
268
 
75
-
269
+ </c:if>
270
+
76
-
271
+ </td>
272
+
273
+ <td>
274
+
275
+ <c:if test="${!empty row.lend_date}">
276
+
277
+
278
+
279
+ <option value=${row.user_no}>${row.user_id}</option>
280
+
281
+ </c:if>
282
+
283
+ </td>
284
+
285
+
286
+
287
+ </c:forEach>
288
+
289
+ </table>
290
+
77
- <br>
291
+ <br>
78
-
79
-
80
-
292
+
81
- <form action="Login.jsp">
293
+ <form action="kanri_mypage.jsp">
294
+
82
-
295
+ <input style="color:white;
296
+
83
- <input style="color: white; background-color: D57200;" type="submit"
297
+ background-color:D57200;" type="submit" name="mypage" value="TOPページ">
84
-
85
- name="reset" value="戻る">
298
+
86
-
299
+
300
+
87
- </form>
301
+ </form>
302
+
303
+
88
304
 
89
305
  </body>
90
306
 
@@ -124,161 +340,233 @@
124
340
 
125
341
  import javax.servlet.http.HttpServletResponse;
126
342
 
343
+ import javax.servlet.http.HttpSession;
344
+
127
345
  import javax.sql.DataSource;
128
346
 
129
347
 
130
348
 
349
+
350
+
131
- @WebServlet("/newuser_tsuika")
351
+ @WebServlet("/book_lend")
132
-
352
+
133
- public class newuser_tsuika extends HttpServlet {
353
+ public class book_lend extends HttpServlet {
134
354
 
135
355
  private static final long serialVersionUID = 1L;
136
356
 
137
357
 
138
358
 
139
- protected void doPost(HttpServletRequest request,
140
-
141
- HttpServletResponse response) throws ServletException, IOException {
359
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
360
+
361
+ IOException {
142
362
 
143
363
  request.setCharacterEncoding("UTF-8");
144
364
 
145
-
365
+ String bookNo = request.getParameter("book_no");
366
+
367
+
368
+
146
-
369
+ HttpSession session = request.getSession();
370
+
371
+ Boolean userResult = (Boolean) session.getAttribute("userResult");
372
+
373
+
374
+
375
+
376
+
147
- Connection con = null;
377
+ Connection con = null;
148
-
378
+
149
- PreparedStatement ps = null;
379
+ PreparedStatement ps = null;
150
-
380
+
151
- String sql = null;
381
+ String sql = null;
152
-
382
+
153
- ResultSet rs = null;
383
+ ResultSet rs = null;
154
-
155
- if (request.getParameter("first_name").equals("")
384
+
156
-
157
- || request.getParameter("last_name").equals("")
385
+
158
-
159
- || request.getParameter("first_name_kana").equals("")
386
+
160
-
161
- || request.getParameter("last_name_kana").equals("")
162
-
163
- || request.getParameter("user_id").equals("")
164
-
165
- || request.getParameter("password").equals("")) {
166
-
167
- request.setAttribute("first_name",
387
+ if(session.getAttribute("userResult")!=null) {
168
-
169
- request.getParameter("first_name"));
388
+
170
-
171
- request.setAttribute("last_name", request.getParameter("last_name"));
172
-
173
- request.setAttribute("first_name_kana",
174
-
175
- request.getParameter("first_name_kanae"));
176
-
177
- request.setAttribute("last_name_kana",
389
+ userResult = (boolean)session.getAttribute("userResult");
178
-
179
- request.getParameter("last_name_kana"));
390
+
180
-
181
- request.setAttribute("user_id", request.getParameter("user_id"));
182
-
183
- request.setAttribute("password", request.getParameter("password"));
184
-
185
-
186
-
187
- request.setAttribute("errorMessage", "未入力項目があります");
188
-
189
-
190
-
191
- // ログイン画面遷移
391
+ }
192
-
193
- this.getServletContext().getRequestDispatcher("/shinki.jsp")
392
+
194
-
393
+
394
+
195
- .forward(request, response);
395
+ if(userResult) {
196
-
197
-
198
-
199
- } else {
396
+
200
-
397
+
398
+
399
+
400
+
201
- try {
401
+ try{
202
-
203
- // データベースの接続を確立
402
+
403
+
204
404
 
205
405
  InitialContext initContext = new InitialContext();
206
406
 
207
- DataSource ds = (DataSource) initContext
208
-
209
- .lookup("java:comp/env/jdbc/Library");
407
+ DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library");
210
408
 
211
409
  con = ds.getConnection();
212
410
 
213
- // SQL
411
+
214
-
215
- sql = "INSERT INTO user (first_name,last_name,first_name_kana,last_name_kana,user_id,password,admin)"
412
+
216
-
217
- + "VALUES (?,?,?,?,?,?,0)";
413
+ sql = "delete from lend where book_no = ?";
218
-
219
-
220
-
221
- // UPDATE命令の準備
222
414
 
223
415
  ps = con.prepareStatement(sql);
224
416
 
225
- // UPDATE命令にポストデータの内容をセット
417
+
226
-
227
- ps.setString(1, request.getParameter("first_name"));
418
+
228
-
229
- ps.setString(2, request.getParameter("last_name"));
419
+
230
-
231
- ps.setString(3, request.getParameter("first_name_kana"));
420
+
232
-
233
- ps.setString(4, request.getParameter("last_name_kana"));
234
-
235
- ps.setString(5, request.getParameter("user_id"));
236
-
237
- ps.setString(6, request.getParameter("password"));
421
+ ps.setString(1, bookNo);
238
-
239
-
240
-
241
- // UPDATE命令を実行
422
+
423
+
242
424
 
243
425
  ps.executeUpdate();
244
426
 
245
-
246
-
247
- // UPDATE命令後の画面遷移
248
-
249
- this.getServletContext().getRequestDispatcher("/shinki.jsp")
250
-
251
- .forward(request, response);
252
-
253
-
254
-
255
- } catch (Exception e) {
427
+ }catch(Exception e){
256
428
 
257
429
  throw new ServletException(e);
258
430
 
259
- } finally {
431
+ }finally{
260
-
432
+
261
- try {
433
+ try{
262
-
434
+
263
- if (rs != null) {
435
+ if(rs != null){
264
436
 
265
437
  rs.close();
266
438
 
267
439
  }
268
440
 
441
+ if(con != null){
442
+
443
+ con.close();
444
+
445
+ }
446
+
269
- if (ps != null) {
447
+ if(ps != null){
270
448
 
271
449
  ps.close();
272
450
 
273
451
  }
274
452
 
453
+ }catch(Exception e){
454
+
455
+
456
+
457
+ }
458
+
459
+ this.getServletContext().getRequestDispatcher("/book_lend.jsp").forward(request, response);
460
+
461
+ }
462
+
463
+ }
464
+
465
+ }
466
+
467
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
468
+
469
+
470
+
471
+ request.setCharacterEncoding("UTF-8");
472
+
473
+
474
+
475
+ String genreNo = request.getParameter("genre_no");
476
+
477
+ String bookNo = request.getParameter("book_no");
478
+
479
+
480
+
481
+ HttpSession session = request.getSession();
482
+
483
+ Boolean userResult = false;
484
+
485
+
486
+
487
+ Connection con = null;
488
+
489
+ PreparedStatement ps = null;
490
+
491
+ String sql = null;
492
+
493
+ ResultSet rs = null;
494
+
495
+
496
+
497
+ if(session.getAttribute("userResult")!=null) {
498
+
499
+ userResult = (boolean)session.getAttribute("userResult");
500
+
501
+ }
502
+
503
+
504
+
505
+ if(userResult) {
506
+
507
+
508
+
509
+ try{
510
+
511
+ InitialContext initContext = new InitialContext();
512
+
513
+ DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library");
514
+
515
+ con = ds.getConnection();
516
+
517
+
518
+
519
+ sql = "insert into lend (user_no,book_no,lend_date,return_date,genre_no) values (?,?,cast(now() as date) ,cast((now() + interval 6 day) as date),?)";
520
+
521
+ ps = con.prepareStatement(sql);
522
+
523
+ Integer a =(Integer)session.getAttribute("user_no");
524
+
525
+ ps.setInt(1, a);
526
+
527
+ ps.setString(2, bookNo);
528
+
529
+ ps.setString(3, genreNo);
530
+
531
+
532
+
533
+
534
+
535
+ ps.executeUpdate();
536
+
537
+ }catch(Exception e){
538
+
539
+ throw new ServletException(e);
540
+
541
+ }finally{
542
+
543
+ try{
544
+
545
+ if(rs != null){
546
+
547
+ rs.close();
548
+
549
+ }
550
+
275
- if (con != null) {
551
+ if(con != null){
276
-
552
+
277
- con.close();
553
+ con.close();
554
+
555
+ }
556
+
557
+ if(ps != null){
558
+
559
+ ps.close();
560
+
561
+ }
562
+
563
+ }catch(Exception e){
564
+
565
+
278
566
 
279
567
  }
280
568
 
281
- } catch (Exception e) {
569
+ this.getServletContext().getRequestDispatcher("/book_lend.jsp").forward(request, response);
282
570
 
283
571
  }
284
572
 
@@ -286,13 +574,7 @@
286
574
 
287
575
 
288
576
 
289
- }
577
+ }
290
-
291
-
292
-
293
- }
294
-
295
-
296
578
 
297
579
  }
298
580