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

質問編集履歴

2

変更

2016/06/20 02:13

投稿

l_l_l_l_l_l_l_l
l_l_l_l_l_l_l_l

スコア38

title CHANGED
@@ -1,1 +1,1 @@
1
- henkou java.lang.NullPointerExceptio
1
+ java.lang.NullPointerExceptio
body 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

title CHANGED
@@ -1,1 +1,1 @@
1
- JSP画面でエラーメッセージが表示されない
1
+ henkou java.lang.NullPointerExceptio
body CHANGED
@@ -1,47 +1,155 @@
1
1
  ###前提・実現したいこと
2
- ユーザの新規登録の際、入力欄に一つでも未入力の箇所ある場合
3
- エラーメッセージを表示させたいのですされせん。
2
+ エラーが出てし
4
-
3
+ 重大: Servlet.service() for servlet [servlet.book_lend] in context with path [/Library] threw exception [java.lang.NullPointerException] with root cause
5
-
6
4
  ###JSP
7
5
  ```
8
6
  <%@ page contentType="text/html; charset=UTF-8"
9
- import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*"%>
7
+ import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*,java.util.Date,java.util.Calendar"%>
10
8
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
11
9
  <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
12
10
 
11
+ <html>
12
+
13
13
  <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"
14
14
  url="jdbc:mysql://localhost/Library" user="root" password="" />
15
+ <sql:query var="rs" dataSource="${db}">
16
+ SELECT book.book_no,book.book_name,book.state,genre.genre_no,genre.genre_name,user.user_no,user.user_id,
17
+ lend.lend_date,lend.return_date FROM book LEFT JOIN lend ON book.book_no = lend.book_no LEFT JOIN
18
+ user ON user.user_no = lend.user_no LEFT JOIN genre ON book.genre_no = genre.genre_no
19
+
20
+ <c:if test="${!empty param['genreNo']}">
21
+ where book.genre_no = ?
22
+ <sql:param value="${param['genreNo']}" />
23
+ ORDER BY book.book_no asc;
15
- <html>
24
+ </c:if>
25
+
26
+ </sql:query>
27
+ <sql:query var="rs1" dataSource="${db}">
28
+ select * from genre
29
+ </sql:query>
30
+
31
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
32
+
16
33
  <head>
17
- <link rel="stylesheet" type="text/css" href="css/Library.css">
18
34
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
19
-
35
+ <link rel="stylesheet" type="text/css" href="css/Lending.css">
20
- <title>新規ユーザ登録画面</title>
36
+ <title>書籍貸出画面</title>
21
37
  </head>
22
38
  <body>
23
- <h2>新規ユーザ登録画面</h2>
39
+ <h2>書籍貸出画面</h2>
24
40
 
41
+ <br>
25
- <form method="POST" action="newuser_tsuika">
42
+ <form method="POST" action="book_lend.jsp">
43
+ <p>
44
+ ジャンル
45
+ <select name="genreNo">
26
- <h3>新規ユーザ追加</h3>
46
+ <option value="">全て</option>
27
- 名前(姓) <input type="text" name="first_name" size="30" >
28
- 名前(名) <input type="text" name="last_name" size="30"> <br>
29
- カナ(姓) <input type="text" name="first_name_kana" size="30">
30
- カナ(名) <input type="text" name="last_name_kana" size="30"> <br>
31
- ID <input type="text" name="user_id" size="30">
47
+ <c:forEach var="row" items="${rs1.rows}">
32
- パスワード <input type="password" name="password" size="30"> <br> <br>
33
- <!-- <input name="admin" type="radio" value="0"></input> --> <input
48
+ <option value=${row.genre_no}>${row.genre_name}</option>
34
- style="color: white; background-color: D57200;" type="submit"
49
+ </c:forEach>
50
+ </select>
35
- value="登録" />
51
+ </p>
36
52
 
53
+ <INPUT style="color:white;
54
+ background-color:D57200;" type="submit" value="絞り込む" />
55
+ </form>
56
+ <br>
57
+
58
+ <table border="1">
59
+ <tr>
60
+ <th>ID</th>
61
+ <th>書籍名</th>
62
+ <th>ジャンル</th>
63
+ <th>貸出日</th>
64
+ <th>返却予定日</th>
65
+ <th>状態</th>
66
+ <th>貸出/返却</th>
67
+ <th>貸りた人</th>
68
+ </tr>
69
+
70
+ <c:forEach var="row" items="${rs.rows}">
71
+ <tr>
72
+ <td>${row.book_no}</td>
73
+ <td>${row.book_name}</td>
74
+ <td>${row.genre_name}</td>
75
+
76
+ <td>
77
+ <c:if test="${empty row.lend_date}">-</c:if>
78
+ ${row.lend_date }
79
+ </td>
80
+
81
+ <td>
82
+ <c:if test="${empty row.lend_date}">-</c:if>
83
+ ${row.return_date }
84
+ </td>
85
+ <td>
86
+
87
+ <c:if test="${row.state == 2}" >
88
+ <span class="fontRed">貸出不可</span>
89
+ </c:if>
90
+ <c:if test="${row.state == 1 }">
91
+ <c:if test="${empty row.lend_date}">
92
+ 貸出可能
93
+ </c:if>
94
+ <c:if test="${!empty row.lend_date}">
95
+ <c:set var= "today" value="<%=new Date()%>"/>
96
+ <c:if test="${today > row.return_date}">
97
+ 返却遅延
98
+ </c:if>
99
+ <c:if test="${today <= row.return_date}">
100
+ 貸出中
101
+ </c:if>
102
+ </c:if>
103
+ </c:if>
104
+
105
+ </td>
106
+
107
+
108
+
109
+ <td>
110
+ <c:if test="${empty row.lend_date}">
111
+ <c:if test="${row.state == 2}">-</c:if>
112
+ <c:if test="${row.state == 1}">
113
+ <form method="POST" action="book_lend">
114
+
115
+ <input type="hidden" value="${row.book_no}" name="book_no">
116
+
117
+
118
+
119
+ <input type="hidden" value="${row.genre_no}" name="genre_no">
120
+
121
+
122
+ <INPUT style="color:white;
123
+ background-color:D57200; width:60px; height:40px;" type="submit" value="貸出" />
124
+ </form>
125
+ </c:if>
126
+ </c:if>
127
+
128
+
129
+ <c:if test="${!empty row.lend_date}">
130
+ <form method="GET" action="book_lend">
131
+ <input type="hidden" value=${row.book_no} name="book_no" />
132
+ <INPUT style="color:white;
133
+ background-color:AFD170; width:60px; height:40px;" type="submit" value="返却" />
37
134
  </form>
135
+ </c:if>
136
+ </td>
137
+ <td>
138
+ <c:if test="${!empty row.lend_date}">
38
139
 
140
+ <option value=${row.user_no}>${row.user_id}</option>
141
+ </c:if>
39
- <br>
142
+ </td>
40
143
 
144
+ </c:forEach>
145
+ </table>
146
+ <br>
41
- <form action="Login.jsp">
147
+ <form action="kanri_mypage.jsp">
148
+ <input style="color:white;
42
- <input style="color: white; background-color: D57200;" type="submit"
149
+ background-color:D57200;" type="submit" name="mypage" value="TOPページ">
43
- name="reset" value="戻る">
150
+
44
- </form>
151
+ </form>
152
+
45
153
  </body>
46
154
  </html>
47
155
  ```
@@ -61,91 +169,124 @@
61
169
  import javax.servlet.http.HttpServlet;
62
170
  import javax.servlet.http.HttpServletRequest;
63
171
  import javax.servlet.http.HttpServletResponse;
172
+ import javax.servlet.http.HttpSession;
64
173
  import javax.sql.DataSource;
65
174
 
175
+
66
- @WebServlet("/newuser_tsuika")
176
+ @WebServlet("/book_lend")
67
- public class newuser_tsuika extends HttpServlet {
177
+ public class book_lend extends HttpServlet {
68
178
  private static final long serialVersionUID = 1L;
69
179
 
70
- protected void doPost(HttpServletRequest request,
71
- HttpServletResponse response) throws ServletException, IOException {
180
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
181
+ IOException {
72
182
  request.setCharacterEncoding("UTF-8");
183
+ String bookNo = request.getParameter("book_no");
73
184
 
74
- Connection con = null;
75
- PreparedStatement ps = null;
76
- String sql = null;
77
- ResultSet rs = null;
78
- if (request.getParameter("first_name").equals("")
79
- || request.getParameter("last_name").equals("")
80
- || request.getParameter("first_name_kana").equals("")
81
- || request.getParameter("last_name_kana").equals("")
82
- || request.getParameter("user_id").equals("")
83
- || request.getParameter("password").equals("")) {
84
- request.setAttribute("first_name",
85
- request.getParameter("first_name"));
185
+ HttpSession session = request.getSession();
86
- request.setAttribute("last_name", request.getParameter("last_name"));
87
- request.setAttribute("first_name_kana",
88
- request.getParameter("first_name_kanae"));
89
- request.setAttribute("last_name_kana",
90
- request.getParameter("last_name_kana"));
91
- request.setAttribute("user_id", request.getParameter("user_id"));
186
+ Boolean userResult = (Boolean) session.getAttribute("userResult");
92
- request.setAttribute("password", request.getParameter("password"));
93
187
 
94
- request.setAttribute("errorMessage", "未入力項目があります");
95
188
 
96
- // ログイン画面遷移
189
+ Connection con = null;
97
- this.getServletContext().getRequestDispatcher("/shinki.jsp")
98
- .forward(request, response);
190
+ PreparedStatement ps = null;
191
+ String sql = null;
192
+ ResultSet rs = null;
99
193
 
194
+ if(session.getAttribute("userResult")!=null) {
195
+ userResult = (boolean)session.getAttribute("userResult");
196
+ }
197
+
100
- } else {
198
+ if(userResult) {
199
+
200
+
101
- try {
201
+ try{
102
- // データベースの接続を確立
202
+
103
203
  InitialContext initContext = new InitialContext();
104
- DataSource ds = (DataSource) initContext
105
- .lookup("java:comp/env/jdbc/Library");
204
+ DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library");
106
205
  con = ds.getConnection();
107
- // SQL
108
- sql = "INSERT INTO user (first_name,last_name,first_name_kana,last_name_kana,user_id,password,admin)"
109
- + "VALUES (?,?,?,?,?,?,0)";
110
206
 
111
- // UPDATE命令の準備
207
+ sql = "delete from lend where book_no = ?";
112
208
  ps = con.prepareStatement(sql);
113
- // UPDATE命令にポストデータの内容をセット
114
- ps.setString(1, request.getParameter("first_name"));
115
- ps.setString(2, request.getParameter("last_name"));
116
- ps.setString(3, request.getParameter("first_name_kana"));
117
- ps.setString(4, request.getParameter("last_name_kana"));
118
- ps.setString(5, request.getParameter("user_id"));
119
- ps.setString(6, request.getParameter("password"));
120
209
 
121
- // UPDATE命令を実行
122
- ps.executeUpdate();
123
210
 
124
- // UPDATE命令後の画面遷移
125
- this.getServletContext().getRequestDispatcher("/shinki.jsp")
126
- .forward(request, response);
211
+ ps.setString(1, bookNo);
127
212
 
213
+ ps.executeUpdate();
128
- } catch (Exception e) {
214
+ }catch(Exception e){
129
215
  throw new ServletException(e);
130
- } finally {
216
+ }finally{
131
- try {
217
+ try{
132
- if (rs != null) {
218
+ if(rs != null){
133
219
  rs.close();
134
220
  }
221
+ if(con != null){
222
+ con.close();
223
+ }
135
- if (ps != null) {
224
+ if(ps != null){
136
225
  ps.close();
137
226
  }
138
- if (con != null) {
139
- con.close();
140
- }
141
- } catch (Exception e) {
227
+ }catch(Exception e){
228
+
142
229
  }
230
+ this.getServletContext().getRequestDispatcher("/book_lend.jsp").forward(request, response);
143
231
  }
144
-
145
232
  }
146
-
147
233
  }
234
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
148
235
 
236
+ request.setCharacterEncoding("UTF-8");
237
+
238
+ String genreNo = request.getParameter("genre_no");
239
+ String bookNo = request.getParameter("book_no");
240
+
241
+ HttpSession session = request.getSession();
242
+ Boolean userResult = false;
243
+
244
+ Connection con = null;
245
+ PreparedStatement ps = null;
246
+ String sql = null;
247
+ ResultSet rs = null;
248
+
249
+ if(session.getAttribute("userResult")!=null) {
250
+ userResult = (boolean)session.getAttribute("userResult");
251
+ }
252
+
253
+ if(userResult) {
254
+
255
+ try{
256
+ InitialContext initContext = new InitialContext();
257
+ DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library");
258
+ con = ds.getConnection();
259
+
260
+ 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),?)";
261
+ ps = con.prepareStatement(sql);
262
+ Integer a =(Integer)session.getAttribute("user_no");
263
+ ps.setInt(1, a);
264
+ ps.setString(2, bookNo);
265
+ ps.setString(3, genreNo);
266
+
267
+
268
+ ps.executeUpdate();
269
+ }catch(Exception e){
270
+ throw new ServletException(e);
271
+ }finally{
272
+ try{
273
+ if(rs != null){
274
+ rs.close();
275
+ }
276
+ if(con != null){
277
+ con.close();
278
+ }
279
+ if(ps != null){
280
+ ps.close();
281
+ }
282
+ }catch(Exception e){
283
+
284
+ }
285
+ this.getServletContext().getRequestDispatcher("/book_lend.jsp").forward(request, response);
286
+ }
287
+ }
288
+
289
+ }
149
290
  }
150
291
 
151
292
  ```