質問編集履歴
1
自己解決した
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,545 +1 @@
|
|
1
|
-
```### 前提・実現したいこと
|
2
|
-
|
3
|
-
DBにデータの登録を行いたい
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
閲覧ありがとうございます。
|
8
|
-
|
9
|
-
Java初心者です。
|
10
|
-
|
11
|
-
画像の投稿掲示板を作成しています。
|
12
|
-
|
13
|
-
具体的には、登録画面で写真、写真の名称、パスコード、投稿者のコメントなどを登録できるようにしています。
|
14
|
-
|
15
|
-
写真以外の値はServlet1でセッションスコープに格納しServlet2でセッションスコープから取り出し写真とともに
|
16
|
-
|
17
|
-
DBに登録処理を行っています。
|
18
|
-
|
19
|
-
しかしながら登録処理を実装中に下記のエラーが発生しました。
|
20
|
-
|
21
|
-
値の
|
1
|
+
こちらの質問に関しては、うまく値の引継ぎが行えていなかったことが発覚したため自己解決しました。
|
22
|
-
|
23
|
-
ご教授いただければ幸いです。
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
```
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
### 発生している問題・エラーメッセージ
|
34
|
-
|
35
|
-
```
|
36
|
-
|
37
|
-
重大: サーブレット [servlet.PhotoServlet] のServlet.service()が例外を投げました
|
38
|
-
|
39
|
-
java.lang.NullPointerException
|
40
|
-
|
41
|
-
at dao.PostDao.insert(PostDao.java:56)
|
42
|
-
|
43
|
-
at servlet.PhotoServlet.doPost(PhotoServlet.java:68)
|
44
|
-
|
45
|
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
|
46
|
-
|
47
|
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
|
48
|
-
|
49
|
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
|
50
|
-
|
51
|
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
|
52
|
-
|
53
|
-
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
|
54
|
-
|
55
|
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
|
56
|
-
|
57
|
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
|
58
|
-
|
59
|
-
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
|
60
|
-
|
61
|
-
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
|
62
|
-
|
63
|
-
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
|
64
|
-
|
65
|
-
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
|
66
|
-
|
67
|
-
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
|
68
|
-
|
69
|
-
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
|
70
|
-
|
71
|
-
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
|
72
|
-
|
73
|
-
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
|
74
|
-
|
75
|
-
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
|
76
|
-
|
77
|
-
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
|
78
|
-
|
79
|
-
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
|
80
|
-
|
81
|
-
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
|
82
|
-
|
83
|
-
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
|
84
|
-
|
85
|
-
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
|
86
|
-
|
87
|
-
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
|
88
|
-
|
89
|
-
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
|
90
|
-
|
91
|
-
at java.lang.Thread.run(Thread.java:748)
|
92
|
-
|
93
|
-
```
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
### 該当のソースコード
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
```Servlet1
|
102
|
-
|
103
|
-
package servlet;
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
import java.io.IOException;
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
import javax.servlet.RequestDispatcher;
|
112
|
-
|
113
|
-
import javax.servlet.ServletException;
|
114
|
-
|
115
|
-
import javax.servlet.annotation.WebServlet;
|
116
|
-
|
117
|
-
import javax.servlet.http.HttpServlet;
|
118
|
-
|
119
|
-
import javax.servlet.http.HttpServletRequest;
|
120
|
-
|
121
|
-
import javax.servlet.http.HttpServletResponse;
|
122
|
-
|
123
|
-
import javax.servlet.http.HttpSession;
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
import dto.PostDto;
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
/**
|
132
|
-
|
133
|
-
* Servlet implementation class RegisterServlet
|
134
|
-
|
135
|
-
*/
|
136
|
-
|
137
|
-
@WebServlet("/RegisterServlet")
|
138
|
-
|
139
|
-
public class RegisterServlet extends HttpServlet {
|
140
|
-
|
141
|
-
private static final long serialVersionUID = 1L;
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
/**
|
146
|
-
|
147
|
-
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
148
|
-
|
149
|
-
*/
|
150
|
-
|
151
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
152
|
-
|
153
|
-
throws ServletException, IOException {
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
request.setCharacterEncoding("UTF-8");
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
// フォームからの入力情報取得
|
162
|
-
|
163
|
-
//写真タイトル
|
164
|
-
|
165
|
-
String photo_title = request.getParameter("photo_title");
|
166
|
-
|
167
|
-
//投稿者名
|
168
|
-
|
169
|
-
String register_name = request.getParameter("register_name");
|
170
|
-
|
171
|
-
//パスコード
|
172
|
-
|
173
|
-
String passcode = request.getParameter("passcode");
|
174
|
-
|
175
|
-
//コメント
|
176
|
-
|
177
|
-
String comment = request.getParameter("comment");
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
int good=0;
|
182
|
-
|
183
|
-
PostDto post = new PostDto(photo_title, register_name, passcode, comment,good);
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
//セッションスコープ
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
HttpSession session = request.getSession();
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
session.setAttribute("post", post);
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/PhotoServlet");
|
200
|
-
|
201
|
-
rd.forward(request, response);
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
```
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
```Servlet2
|
218
|
-
|
219
|
-
package servlet;
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
import java.io.File;
|
224
|
-
|
225
|
-
import java.io.IOException;
|
226
|
-
|
227
|
-
import java.nio.file.Path;
|
228
|
-
|
229
|
-
import java.nio.file.Paths;
|
230
|
-
|
231
|
-
import java.sql.SQLException;
|
232
|
-
|
233
|
-
import java.util.Arrays;
|
234
|
-
|
235
|
-
import java.util.stream.Collectors;
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
import javax.servlet.ServletException;
|
240
|
-
|
241
|
-
import javax.servlet.annotation.MultipartConfig;
|
242
|
-
|
243
|
-
import javax.servlet.annotation.WebServlet;
|
244
|
-
|
245
|
-
import javax.servlet.http.HttpServlet;
|
246
|
-
|
247
|
-
import javax.servlet.http.HttpServletRequest;
|
248
|
-
|
249
|
-
import javax.servlet.http.HttpServletResponse;
|
250
|
-
|
251
|
-
import javax.servlet.http.HttpSession;
|
252
|
-
|
253
|
-
import javax.servlet.http.Part;
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
import dao.PostDao;
|
258
|
-
|
259
|
-
import dto.PostDto;
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
/**
|
264
|
-
|
265
|
-
* Servlet implementation class PhotoServlet
|
266
|
-
|
267
|
-
*/
|
268
|
-
|
269
|
-
@WebServlet("/PhotoServlet")
|
270
|
-
|
271
|
-
@MultipartConfig
|
272
|
-
|
273
|
-
public class PhotoServlet extends HttpServlet {
|
274
|
-
|
275
|
-
private static final long serialVersionUID = 1L;
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
/**
|
280
|
-
|
281
|
-
* @see HttpServlet#HttpServlet()
|
282
|
-
|
283
|
-
*/
|
284
|
-
|
285
|
-
public PhotoServlet() {
|
286
|
-
|
287
|
-
super();
|
288
|
-
|
289
|
-
// TODO Auto-generated constructor stub
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
/**
|
296
|
-
|
297
|
-
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
298
|
-
|
299
|
-
*/
|
300
|
-
|
301
|
-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
302
|
-
|
303
|
-
throws ServletException, IOException {
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
/**
|
312
|
-
|
313
|
-
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
314
|
-
|
315
|
-
*/
|
316
|
-
|
317
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
318
|
-
|
319
|
-
throws ServletException, IOException {
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
request.setCharacterEncoding("UTF-8");
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
String path = "null";
|
328
|
-
|
329
|
-
String destination = request.getParameter("destination");
|
330
|
-
|
331
|
-
Part part = request.getPart("file");
|
332
|
-
|
333
|
-
String fileName = getFielName(part);
|
334
|
-
|
335
|
-
Path photo = Paths.get(destination + File.separator + fileName);
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
try {
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
HttpSession session = request.getSession();
|
344
|
-
|
345
|
-
PostDto post = (PostDto) session.getAttribute("post");
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
System.out.println(post);
|
350
|
-
|
351
|
-
PostDao postDao = new PostDao();
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
postDao.insert(post, photo);
|
356
|
-
|
357
|
-
path = "/jsp/registerPhotoComplete.jsp";
|
358
|
-
|
359
|
-
} catch (SQLException e) {
|
360
|
-
|
361
|
-
// TODO 自動生成された catch ブロック
|
362
|
-
|
363
|
-
e.printStackTrace();
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
}
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
private String getFielName(Part part) {
|
372
|
-
|
373
|
-
String header = part.getHeader("content-disposition");
|
374
|
-
|
375
|
-
System.out.println("***" + header);
|
376
|
-
|
377
|
-
String[] split = header.split(";");
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
String fileName = Arrays.asList(split).stream()
|
382
|
-
|
383
|
-
.filter(s -> s.trim().startsWith("filename"))
|
384
|
-
|
385
|
-
.collect(Collectors.joining());
|
386
|
-
|
387
|
-
return fileName.substring(fileName.indexOf("=") + 1).replace("\"", "");
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
```
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
```SQL
|
398
|
-
|
399
|
-
public int insert(PostDto postDto, Path photo) throws SQLException {
|
400
|
-
|
401
|
-
int result = 0;
|
402
|
-
|
403
|
-
try (Connection conn = DBManager.getConnection();) {
|
404
|
-
|
405
|
-
conn.setAutoCommit(false);
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
String sql = "INSERT INTO post_data VALUES(?,?,now(),?,?,?)";
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
try (PreparedStatement pStmt = conn.prepareStatement(sql);) {
|
414
|
-
|
415
|
-
pStmt.setInt(1, postDto.getPost_id());
|
416
|
-
|
417
|
-
pStmt.setString(2, postDto.getPhoto_title());
|
418
|
-
|
419
|
-
pStmt.setString(3, postDto.getRegister_name());
|
420
|
-
|
421
|
-
pStmt.setString(4, postDto.getComment());
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
result = pStmt.executeUpdate();
|
426
|
-
|
427
|
-
} catch (SQLException e) {
|
428
|
-
|
429
|
-
e.printStackTrace();
|
430
|
-
|
431
|
-
conn.rollback();
|
432
|
-
|
433
|
-
}
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
sql = "INSERT INTO good_countup VALUES(?,?)";
|
438
|
-
|
439
|
-
try (PreparedStatement pStmt = conn.prepareStatement(sql);) {
|
440
|
-
|
441
|
-
pStmt.setInt(1, postDto.getPost_id());
|
442
|
-
|
443
|
-
pStmt.setInt(2, postDto.getGood());
|
444
|
-
|
445
|
-
result = pStmt.executeUpdate();
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
result = pStmt.executeUpdate();
|
450
|
-
|
451
|
-
} catch (SQLException e) {
|
452
|
-
|
453
|
-
e.printStackTrace();
|
454
|
-
|
455
|
-
conn.rollback();
|
456
|
-
|
457
|
-
}
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
sql = "INSERT INTO passcode_data VALUES(?,?)";
|
462
|
-
|
463
|
-
try (PreparedStatement pStmt = conn.prepareStatement(sql);) {
|
464
|
-
|
465
|
-
pStmt.setInt(1, postDto.getPost_id());
|
466
|
-
|
467
|
-
pStmt.setString(2, postDto.getPasscode());
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
result = pStmt.executeUpdate();
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
} catch (SQLException e) {
|
476
|
-
|
477
|
-
e.printStackTrace();
|
478
|
-
|
479
|
-
conn.rollback();
|
480
|
-
|
481
|
-
}
|
482
|
-
|
483
|
-
sql = "INSERT INTO photo_path VALUES(?,?)";
|
484
|
-
|
485
|
-
try (PreparedStatement pStmt = conn.prepareStatement(sql);) {
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
// SQLに検索条件を設定
|
490
|
-
|
491
|
-
pStmt.setInt(1, postDto.getPost_id());
|
492
|
-
|
493
|
-
pStmt.setString(2, postDto.getPhoto());
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
// INSERT文を実行
|
498
|
-
|
499
|
-
result = pStmt.executeUpdate();
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
conn.commit();
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
} catch (SQLException e) {
|
508
|
-
|
509
|
-
e.printStackTrace();
|
510
|
-
|
511
|
-
conn.rollback();
|
512
|
-
|
513
|
-
}
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
} catch (SQLException e) {
|
518
|
-
|
519
|
-
e.printStackTrace();
|
520
|
-
|
521
|
-
}
|
522
|
-
|
523
|
-
return result;
|
524
|
-
|
525
|
-
}
|
526
|
-
|
527
|
-
```
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
### 試したこと
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
ここに問題に対して試したことを記載してください。
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
### 補足情報(FW/ツールのバージョンなど)
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
ここにより詳細な情報を記載してください。
|