質問編集履歴

3

ソースと行ったことに関しての編集

2019/11/19 05:57

投稿

javas
javas

スコア5

test CHANGED
File without changes
test CHANGED
@@ -26,17 +26,11 @@
26
26
 
27
27
  情報: Command line argument: -Dfile.encoding=UTF-8
28
28
 
29
- 情報: Using a shared selector for servlet write/read
30
-
31
- 情報: Using a shared selector for servlet write/read
32
-
33
- 情報: Initialization processed in 2771 ms
34
-
35
29
  情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug
36
30
 
37
31
  logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
38
32
 
39
- 情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
33
+
40
34
 
41
35
 
42
36
 
@@ -180,6 +174,8 @@
180
174
 
181
175
  PrintWriter out = response.getWriter();
182
176
 
177
+ request.setCharacterEncoding("UTF-8");
178
+
183
179
 
184
180
 
185
181
  String target = request.getRequestURI();
@@ -292,6 +288,8 @@
292
288
 
293
289
  PrintWriter out = response.getWriter();
294
290
 
291
+ request.setCharacterEncoding("UTF-8");
292
+
295
293
 
296
294
 
297
295
  out.println("<html>");
@@ -480,6 +478,8 @@
480
478
 
481
479
 
482
480
 
481
+ request.setCharacterEncoding("UTF-8");
482
+
483
483
 
484
484
 
485
485
  String name=request.getParameter("name");
@@ -608,6 +608,10 @@
608
608
 
609
609
  System.out.println(out);
610
610
 
611
+ request.setCharacterEncoding("UTF-8");
612
+
613
+
614
+
611
615
  HttpSession session = request.getSession(true);
612
616
 
613
617
  session.invalidate();
@@ -634,21 +638,37 @@
634
638
 
635
639
  ➂デバッグ
636
640
 
637
- →Eclipse上でソースコード(呼び出しを想定しているメソッドの先頭のステートメント)にブレークポイントを設定してサーバーをデバッグモードで実行しました変数や式の項目に何も表示されませんでした
641
+ →Eclipse上でソースコード(呼び出しを想定しているメソッドの先頭のステートメント)にブレークポイントを設定してサーバーをデバッグモードで実行しました入力したデータはしっかり
642
+
643
+ 入りました。
644
+
645
+
638
646
 
639
647
  ➃サーブレットのString name=request.getParameter("name");
640
648
 
641
649
  String pass=request.getParameter("pass");
642
650
 
651
+ pstmt.setString(1, name);
652
+
653
+ pstmt.setString(2, pass);
654
+
643
655
  でname、passに値を詰めて何が入っているかをSystem.out.printlnで確認した結果
644
656
 
645
- ・name(ユーザー名)は漢字で入力し文字化けで出力されました
657
+ ・name(ユーザー名)は入力した正いデータが出力されました
646
-
658
+
647
- ・pass(パスワード)は数字で入力しnullで出力されました。
659
+ ・pass(パスワード)入力した正いデータが出力されました。
648
660
 
649
661
  ※SQL文のCreateではどちらも文字列コードはUTF-8に設定しました。
650
662
 
651
-
663
+ (対象となっているテーブルを作成した際のSQL文)
664
+
665
+ CREATE TABLE member1 (
666
+
667
+ name varchar(100) DEFAULT NULL,
668
+
669
+ pass varchar(100) DEFAULT NULL
670
+
671
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
652
672
 
653
673
 
654
674
 

2

試してみたことの項目の追加

2019/11/19 05:57

投稿

javas
javas

スコア5

test CHANGED
File without changes
test CHANGED
@@ -26,12 +26,8 @@
26
26
 
27
27
  情報: Command line argument: -Dfile.encoding=UTF-8
28
28
 
29
- 情報: Initializing ProtocolHandler ["http-nio-8080"]
30
-
31
29
  情報: Using a shared selector for servlet write/read
32
30
 
33
- 情報: Initializing ProtocolHandler ["ajp-nio-8009"]
34
-
35
31
  情報: Using a shared selector for servlet write/read
36
32
 
37
33
  情報: Initialization processed in 2771 ms
@@ -632,9 +628,25 @@
632
628
 
633
629
 
634
630
 
635
- SQL文の修正
631
+ SQL文の修正
636
-
632
+
637
- データベースのテーブルの修正
633
+ データベースのテーブルの修正
634
+
635
+ ➂デバッグ
636
+
637
+ →Eclipse上でソースコード(呼び出しを想定しているメソッドの先頭のステートメント)にブレークポイントを設定してサーバーをデバッグモードで実行しましたが、変数や式の項目に何も表示されませんでした。
638
+
639
+ ➃サーブレットのString name=request.getParameter("name");
640
+
641
+ String pass=request.getParameter("pass");
642
+
643
+ でname、passに値を詰めて何が入っているかをSystem.out.printlnで確認した結果
644
+
645
+ ・name(ユーザー名)は漢字で入力しましたが文字化けで出力されました
646
+
647
+ ・pass(パスワード)は数字で入力しましたがnullで出力されました。
648
+
649
+ ※SQL文のCreateではどちらも文字列コードはUTF-8に設定しました。
638
650
 
639
651
 
640
652
 

1

ソースコードの編集

2019/11/19 02:57

投稿

javas
javas

スコア5

test CHANGED
File without changes
test CHANGED
@@ -24,67 +24,23 @@
24
24
 
25
25
  エラーメッセージが出ていないのでコンソールに出力された情報を入れます。
26
26
 
27
- 情報: Server version: Apache Tomcat/8.0.36 [月 11 18 17:15:34 JST 2019]
28
-
29
- 情報: Server built: Jun 9 2016 13:55:50 UTC [月 11 18 17:15:34 JST 2019]
30
-
31
- 情報: Server number: 8.0.36.0 [月 11 18 17:15:34 JST 2019]
32
-
33
- 情報: OS Name: Windows 10 [月 11 18 17:15:34 JST 2019]
34
-
35
- 情報: OS Version: 10.0 [月 11 18 17:15:34 JST 2019]
36
-
37
- 情報: Architecture: amd64 [月 11 18 17:15:34 JST 2019]
38
-
39
- 情報: Java Home: C:\Users\ISHIYAMA-R\pleiades\java\8\jre [月 11 18 17:15:34 JST 2019]
40
-
41
- 情報: JVM Version: 1.8.0_172-b11 [月 11 18 17:15:34 JST 2019]
42
-
43
- 情報: JVM Vendor: Oracle Corporation [月 11 18 17:15:34 JST 2019]
44
-
45
- 情報: CATALINA_BASE: C:\Users\ISHIYAMA-R.metadata.plugins\org.eclipse.wst.server.core\tmp1 [月 11 18 17:15:34 JST 2019]
46
-
47
- 情報: CATALINA_HOME: C:\Users\ISHIYAMA-R\pleiades\tomcat\8 [月 11 18 17:15:34 JST 2019]
48
-
49
- 情報: Command line argument: -Dcatalina.base=C:\Users\ISHIYAMA-R.metadata.plugins\org.eclipse.wst.server.core\tmp1 [月 11 18 17:15:34 JST 2019]
50
-
51
- 情報: Command line argument: -Dcatalina.home=C:\Users\ISHIYAMA-R\pleiades\tomcat\8 [月 11 18 17:15:34 JST 2019]
52
-
53
- 情報: Command line argument: -Dwtp.deploy=C:\Users\ISHIYAMA-R.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps [月 11 18 17:15:34 JST 2019]
54
-
55
- 情報: Command line argument: -Djava.endorsed.dirs=C:\Users\ISHIYAMA-R\pleiades\tomcat\8\endorsed [月 11 18 17:15:34 JST 2019]
56
-
57
- 情報: Command line argument: -Dfile.encoding=UTF-8 [月 11 18 17:15:34 JST 2019]
58
-
59
- 情報: Command line argument: -Xbootclasspath/a:C:\Users\ISHIYAMA-R\mysql-connector-java-5.1.48\mysql-connector-java-5.1.48\mysql-connector-java-5.1.48-bin.jar [月 11 18 17:15:34 JST 2019]
60
-
61
- 情報: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Users\ISHIYAMA-R\pleiades\java\8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Users/ISHIYAMA-R/pleiades/eclipse/jre/bin/../jre/bin/server;C:/Users/ISHIYAMA-R/pleiades/eclipse/jre/bin/../jre/bin;C:/Users/ISHIYAMA-R/pleiades/eclipse/jre/bin/../jre/lib/amd64;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\Docker\Docker\Resources\bin;C:\Program Files (x86)\Python37-32\Scripts\;C:\Program Files (x86)\Python37-32\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files (x86)\Common Files\Apple\Apple Application Support;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\12.0\DLLShared\;C:\Program Files (x86)\Roxio 2010\OEM\AudioCore\;c:\Program Files (x86)\Common Files\Ulead Systems\MPEG;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\nodejs\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Java\jdk1.8.0_211\bin;C:\Users\iTAC work\AppData\Roaming\Python\Python37\Scripts;%CATALINA_HOME%\bin;"C:\Program Files\MySQL\MySQL Utilities 1.6\;";C:\Program Files\MySQL\MySQL Server 5.7\bin;C:\Users\ISHIYAMA-R\AppData\Local\Microsoft\WindowsApps;;C:\Users\ISHIYAMA-R\pleiades\eclipse;;. [月 11 18 17:15:34 JST 2019]
62
-
63
- 情報: Initializing ProtocolHandler ["http-nio-8080"] [月 11 18 17:15:35 JST 2019]
64
-
65
- 情報: Using a shared selector for servlet write/read [月 11 18 17:15:36 JST 2019]
66
-
67
- 情報: Initializing ProtocolHandler ["ajp-nio-8009"] [月 11 18 17:15:36 JST 2019]
68
-
69
- 情報: Using a shared selector for servlet write/read [月 11 18 17:15:36 JST 2019]
70
-
71
- 情報: Initialization processed in 2771 ms [月 11 18 17:15:36 JST 2019]
72
-
73
- 情報: サービス Catalina を起動します [月 11 18 17:15:36 JST 2019]
74
-
75
- 情報: Starting Servlet Engine: Apache Tomcat/8.0.36 [月 11 18 17:15:36 JST 2019]
27
+ 情報: Command line argument: -Dfile.encoding=UTF-8
28
+
29
+ 情報: Initializing ProtocolHandler ["http-nio-8080"]
30
+
31
+ 情報: Using a shared selector for servlet write/read
32
+
33
+ 情報: Initializing ProtocolHandler ["ajp-nio-8009"]
34
+
35
+ 情報: Using a shared selector for servlet write/read
36
+
37
+ 情報: Initialization processed in 2771 ms
76
38
 
77
39
  情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug
78
40
 
79
- logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. [月 11 18 17:15:47 JST 2019]
80
-
81
- 情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. [月 11 18 17:15:55 JST 2019]
82
-
83
- 情報: Starting ProtocolHandler ["http-nio-8080"] [月 11 18 17:15:55 JST 2019]
84
-
85
- 情報: Starting ProtocolHandler ["ajp-nio-8009"] [月 11 18 17:15:55 JST 2019]
86
-
87
- 情報: Server startup in 19837 ms [月 11 18 17:15:56 JST 2019]
41
+ logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
42
+
43
+ 情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
88
44
 
89
45
 
90
46
 
@@ -192,7 +148,9 @@
192
148
 
193
149
 
194
150
 
195
-
151
+ ```
152
+
153
+ ```JavaEE
196
154
 
197
155
  import java.io.IOException;
198
156
 
@@ -294,9 +252,17 @@
294
252
 
295
253
  }
296
254
 
297
-
255
+ ```
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
298
-
265
+ ```javaEE
299
-
300
266
 
301
267
  import java.io.IOException;
302
268
 
@@ -316,7 +282,7 @@
316
282
 
317
283
 
318
284
 
319
- public class CustomAuth2 extends HttpServlet {
285
+ public class Login2 extends HttpServlet {
320
286
 
321
287
 
322
288
 
@@ -332,61 +298,77 @@
332
298
 
333
299
 
334
300
 
301
+ out.println("<html>");
302
+
335
- String target = request.getRequestURI();
303
+ out.println("<head>");
304
+
336
-
305
+ out.println("<title>ログインページ</title>");
306
+
337
-
307
+ out.println("</head>");
308
+
338
-
309
+ out.println("<body>");
310
+
311
+
312
+
313
+ out.println("<h1>ログイン画面</h1>");
314
+
315
+
316
+
339
- HttpSession session = request.getSession(false);
317
+ HttpSession session = request.getSession(true);
318
+
319
+
320
+
340
-
321
+ /* 認証失敗から呼び出されたのかどうか */
322
+
341
-
323
+ Object status = session.getAttribute("status");
342
-
324
+
325
+
326
+
343
- if (session == null){
327
+ if (status != null){
344
-
345
- /* まだ認証されていない */
328
+
346
-
347
- session = request.getSession(true);
329
+ out.println("<p>認証に失敗しました</p>");
330
+
348
-
331
+ out.println("<p>再度ユーザー名とパスワードを入力して下さい</p>");
332
+
333
+
334
+
349
- session.setAttribute("target", target);
335
+ session.setAttribute("status", null);
350
-
351
-
352
-
353
- response.sendRedirect("/auth/Login");
354
-
355
- }else{
356
-
357
- Object loginCheck = session.getAttribute("login");
358
-
359
- if (loginCheck == null){
360
-
361
- /* まだ認証されていない */
362
-
363
- session.setAttribute("target", target);
364
-
365
- response.sendRedirect("/auth/Login");
366
-
367
- }
368
336
 
369
337
  }
370
338
 
371
339
 
372
340
 
341
+ out.println("<form method=\"POST\" action=\"/auth/LoginCheck\" name=\"loginform\">");
342
+
343
+ out.println("<table>");
344
+
373
- out.println("<html>");
345
+ out.println("<tr>");
346
+
374
-
347
+ out.println("<td>ユーザー名</td>");
348
+
349
+ out.println("<td><input type=\"text\" name=\"name\" size=\"32\"></td>");
350
+
351
+ out.println("</tr>");
352
+
375
- out.println("<head>");
353
+ out.println("<tr>");
376
-
354
+
377
- out.println("<title>ザー認証テスト</title>");
355
+ out.println("<td>パスワ</td>");
356
+
378
-
357
+ out.println("<td><input type=\"password\" pass=\"pass\" size=\"32\"></td>");
358
+
359
+ out.println("</tr>");
360
+
361
+ out.println("<tr>");
362
+
363
+ out.println("<td><input type=\"submit\" value=\"login\"></td>");
364
+
365
+ out.println("<td><input type=\"reset\" value=\"reset\"></td>");
366
+
367
+ out.println("</tr>");
368
+
369
+ out.println("</table>");
370
+
379
- out.println("</head>");
371
+ out.println("</form>");
380
-
381
- out.println("<body>");
382
-
383
-
384
-
385
- out.println("<p>テストページ2</p>");
386
-
387
-
388
-
389
- out.println("<p><a href=\"/auth/CustomAuth1\">テストページ1へ</a></p>");
390
372
 
391
373
 
392
374
 
@@ -398,14 +380,252 @@
398
380
 
399
381
  }
400
382
 
401
-
402
-
403
-
404
-
405
-
406
-
407
383
  ```
408
384
 
385
+ ```javaEE
386
+
387
+
388
+
389
+ import java.io.IOException;
390
+
391
+ import java.io.PrintWriter;
392
+
393
+ import java.sql.Connection;
394
+
395
+ import java.sql.DriverManager;
396
+
397
+ import java.sql.PreparedStatement;
398
+
399
+ import java.sql.ResultSet;
400
+
401
+ import java.sql.SQLException;
402
+
403
+
404
+
405
+ import javax.servlet.ServletException;
406
+
407
+ import javax.servlet.http.HttpServlet;
408
+
409
+ import javax.servlet.http.HttpServletRequest;
410
+
411
+ import javax.servlet.http.HttpServletResponse;
412
+
413
+ import javax.servlet.http.HttpSession;
414
+
415
+
416
+
417
+ public class LoginCheck2 extends HttpServlet {
418
+
419
+
420
+
421
+ protected Connection conn= null;
422
+
423
+ public void init() throws ServletException{
424
+
425
+
426
+
427
+ String url = "jdbc:mysql://localhost:3306?characterEncoding=UTF-8&serverTimezone=JST&useSSL=false";
428
+
429
+ String user = "root";
430
+
431
+ String password ="root";
432
+
433
+
434
+
435
+ try {
436
+
437
+ Class.forName("com.mysql.jdbc.Driver").newInstance();
438
+
439
+ conn = DriverManager.getConnection(url, user, password);
440
+
441
+ }catch (ClassNotFoundException e) {
442
+
443
+ log("ClassNotFoundException:" + e.getMessage());
444
+
445
+ }catch(SQLException e) {
446
+
447
+ log("SQLException:" + e.getMessage());
448
+
449
+ }catch (Exception e) {
450
+
451
+ log("Exception:" + e.getMessage());
452
+
453
+ }
454
+
455
+ }
456
+
457
+ public void destory() {
458
+
459
+ try {
460
+
461
+ if(conn != null) {
462
+
463
+ conn.close();
464
+
465
+ }
466
+
467
+ }catch (SQLException e) {
468
+
469
+ log("SQLException:" + e.getMessage());
470
+
471
+ }
472
+
473
+ }
474
+
475
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
476
+
477
+ throws IOException, ServletException{
478
+
479
+ response.setContentType("text/html; charset=UTF-8");
480
+
481
+ PrintWriter out = response.getWriter();
482
+
483
+ System.out.println(out);
484
+
485
+
486
+
487
+
488
+
489
+ String name=request.getParameter("name");
490
+
491
+ String pass=request.getParameter("pass");
492
+
493
+ HttpSession session=request.getSession(true);
494
+
495
+ boolean check = authUser(name, pass);
496
+
497
+ if(check) {
498
+
499
+ session.setAttribute("login", "OK");
500
+
501
+
502
+
503
+ String target=(String)session.getAttribute("target");
504
+
505
+ response.sendRedirect(target);
506
+
507
+ }else {
508
+
509
+ session.setAttribute("status", "Not Auth");
510
+
511
+ response.sendRedirect("/auth/Login");
512
+
513
+ }
514
+
515
+
516
+
517
+ }
518
+
519
+ protected boolean authUser(String name, String pass) {
520
+
521
+ if(name == null || name.length() == 0 || pass == null || pass.length() == 0) {
522
+
523
+ return false;
524
+
525
+ }
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+ try {
534
+
535
+ String sql = "SELECT name, pass FROM rensyu.member1 WHERE name = ? and pass = ? ";
536
+
537
+ PreparedStatement pstmt = conn.prepareStatement(sql);
538
+
539
+ pstmt.setString(1, name);
540
+
541
+ pstmt.setString(2, pass);
542
+
543
+
544
+
545
+ ResultSet rs =pstmt.executeQuery();
546
+
547
+
548
+
549
+ if(rs.next()) {
550
+
551
+ return true;
552
+
553
+
554
+
555
+ }else {
556
+
557
+
558
+
559
+ return false;
560
+
561
+ }
562
+
563
+
564
+
565
+ }catch (SQLException e) {
566
+
567
+ log("SQLException:" + e.getMessage());
568
+
569
+
570
+
571
+ return false;
572
+
573
+ }
574
+
575
+ }
576
+
577
+ }
578
+
579
+ ```
580
+
581
+ ```javaEE
582
+
583
+ import java.io.IOException;
584
+
585
+ import java.io.PrintWriter;
586
+
587
+
588
+
589
+ import javax.servlet.ServletException;
590
+
591
+ import javax.servlet.http.HttpServlet;
592
+
593
+ import javax.servlet.http.HttpServletRequest;
594
+
595
+ import javax.servlet.http.HttpServletResponse;
596
+
597
+ import javax.servlet.http.HttpSession;
598
+
599
+
600
+
601
+ public class Logout1 extends HttpServlet {
602
+
603
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
604
+
605
+ throws IOException, ServletException{
606
+
607
+
608
+
609
+ response.setContentType("text/html; charset=UTF-8");
610
+
611
+ PrintWriter out = response.getWriter();
612
+
613
+ System.out.println(out);
614
+
615
+ HttpSession session = request.getSession(true);
616
+
617
+ session.invalidate();
618
+
619
+
620
+
621
+ response.sendRedirect("/auth/Login");
622
+
623
+ }
624
+
625
+ }
626
+
627
+ ```
628
+
409
629
 
410
630
 
411
631
  ### 試したこと