質問編集履歴
1
質問の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -150,17 +150,11 @@
|
|
150
150
|
|
151
151
|
```
|
152
152
|
|
153
|
-
|
154
|
-
|
155
|
-
###
|
153
|
+
### 20200909追記
|
156
|
-
|
154
|
+
|
157
|
-
|
155
|
+
storm3様のご回答により、URL直打ちでのタブ切り替えはできたのですが、
|
158
|
-
|
159
|
-
|
160
|
-
|
156
|
+
|
161
|
-
|
157
|
+
ServletにてgetRequestDispatcherを使って#registを指定してもアンカー部が無視された状態になってしまいます。
|
162
|
-
|
163
|
-
|
164
158
|
|
165
159
|
StudentEditServlet.java
|
166
160
|
|
@@ -170,204 +164,458 @@
|
|
170
164
|
|
171
165
|
```
|
172
166
|
|
173
|
-
|
167
|
+
↓eclipseエラー
|
168
|
+
|
174
|
-
|
169
|
+
![イメージ説明](95ffd53368800050673d84099a24c0f5.png)
|
170
|
+
|
175
|
-
[こちらのサイト](https://t
|
171
|
+
[こちらのサイト](https://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=32609&forum=12)を拝見したところ、Servletでこのようにハッシュを指定して画面遷移することは難しいように見受けられました。
|
172
|
+
|
176
|
-
|
173
|
+
JavaScriptであればアンカー部指定の画面遷移が可能かと思いますが、内部処理自体はjavaで行っているため、
|
174
|
+
|
177
|
-
|
175
|
+
「値渡し自体はJavaで行いつつ、画面遷移部分のみJavaScriptに任せる」ということは可能なのでしょうか。
|
176
|
+
|
177
|
+
こちらの解決方法につきまして、ご教示いただけますと幸いです。
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
StudentBeans.java
|
178
182
|
|
179
183
|
```ここに言語を入力
|
180
184
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
s
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
sr
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
185
|
+
//import省略
|
186
|
+
|
187
|
+
public class StudentBeans {
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
private static String student_id;
|
192
|
+
|
193
|
+
private static String student_name;
|
194
|
+
|
195
|
+
private static String student_furigana;
|
196
|
+
|
197
|
+
private static String school_year;
|
198
|
+
|
199
|
+
private static String school_class;
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
private static Connection conn = null;
|
204
|
+
|
205
|
+
private static PreparedStatement ps = null;
|
206
|
+
|
207
|
+
private static ResultSet result = null;
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
static String user = "suser";
|
212
|
+
|
213
|
+
static String password = "spass";
|
214
|
+
|
215
|
+
static String url = "jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false";
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
public StudentBeans(HttpServletRequest request) {
|
220
|
+
|
221
|
+
setStudent_id(request.getParameter("student_id"));
|
222
|
+
|
223
|
+
setStudent_name(request.getParameter("student_name"));
|
224
|
+
|
225
|
+
setStudent_furigana(request.getParameter("student_furigana"));
|
226
|
+
|
227
|
+
setSchool_year(request.getParameter("school_year"));
|
228
|
+
|
229
|
+
setSchool_class(request.getParameter("school_class"));
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
public static String getStudent_id() {
|
236
|
+
|
237
|
+
return student_id;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
public static void setStudent_id(String student_id) {
|
242
|
+
|
243
|
+
StudentBeans.student_id = student_id;
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
public String getStudent_name() {
|
248
|
+
|
249
|
+
return student_name;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
public static void setStudent_name(String student_name) {
|
254
|
+
|
255
|
+
StudentBeans.student_name = student_name;
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
public String getStudent_furigana() {
|
260
|
+
|
261
|
+
return student_furigana;
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
public static void setStudent_furigana(String student_furigana) {
|
266
|
+
|
267
|
+
StudentBeans.student_furigana = student_furigana;
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
public String getSchool_year() {
|
272
|
+
|
273
|
+
return school_year;
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
public static void setSchool_year(String school_year) {
|
278
|
+
|
279
|
+
StudentBeans.school_year = school_year;
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
public String getSchool_class() {
|
284
|
+
|
285
|
+
return school_class;
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
public static void setSchool_class(String school_class) {
|
290
|
+
|
291
|
+
StudentBeans.school_class = school_class;
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
public boolean registData() {
|
298
|
+
|
299
|
+
try {
|
300
|
+
|
301
|
+
Class.forName("com.mysql.jdbc.Driver");
|
302
|
+
|
303
|
+
conn = DriverManager.getConnection(url, user, password);
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
if(student_id.equals("")) {//生徒ID自動採番
|
308
|
+
|
309
|
+
String sql = "insert into student(student_name, student_furigana, school_year, school_class) values (?, ?, ?, ?)";
|
310
|
+
|
311
|
+
ps = conn.prepareStatement(sql);
|
312
|
+
|
313
|
+
ps.setString(1, student_name);
|
314
|
+
|
315
|
+
ps.setString(2, student_furigana);
|
316
|
+
|
317
|
+
ps.setInt(3, Integer.parseInt(school_year));
|
318
|
+
|
319
|
+
ps.setInt(4, Integer.parseInt(school_class));
|
320
|
+
|
321
|
+
} else {//生徒ID指定
|
322
|
+
|
323
|
+
String sql = "insert into student(student_id, student_name, student_furigana, school_year, school_class) values (?, ?, ?, ?, ?)";
|
324
|
+
|
325
|
+
ps = conn.prepareStatement(sql);
|
326
|
+
|
327
|
+
ps.setInt(1, Integer.parseInt(student_id));
|
328
|
+
|
329
|
+
ps.setString(2, student_name);
|
330
|
+
|
331
|
+
ps.setString(3, student_furigana);
|
332
|
+
|
333
|
+
ps.setInt(4, Integer.parseInt(school_year));
|
334
|
+
|
335
|
+
ps.setInt(5, Integer.parseInt(school_class));
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
ps.executeUpdate();
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
return true;
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
} catch(Exception e) {
|
350
|
+
|
351
|
+
e.printStackTrace();
|
352
|
+
|
353
|
+
return false;
|
354
|
+
|
355
|
+
} finally {
|
356
|
+
|
357
|
+
if(conn != null) {
|
358
|
+
|
359
|
+
try {
|
360
|
+
|
361
|
+
conn.close();
|
362
|
+
|
363
|
+
} catch (SQLException e) {
|
364
|
+
|
365
|
+
e.printStackTrace();
|
366
|
+
|
367
|
+
System.out.println("MySQLのクローズに失敗しました。");
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
public static boolean isNumber(String val) {
|
382
|
+
|
383
|
+
String regex = "\A[-]?[0-9]+\z";
|
384
|
+
|
385
|
+
Pattern p = Pattern.compile(regex);
|
386
|
+
|
387
|
+
Matcher m1 = p.matcher(val);
|
388
|
+
|
389
|
+
return m1.find();
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
public boolean isFurigana(String student_furigana) {
|
396
|
+
|
397
|
+
String regularExpression = "^[ァ-ヶー]+$";
|
398
|
+
|
399
|
+
Pattern pattern = Pattern.compile(regularExpression);
|
400
|
+
|
401
|
+
Matcher matcher = pattern.matcher(student_furigana);
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
if(matcher.matches()) {
|
406
|
+
|
407
|
+
return true;
|
408
|
+
|
409
|
+
} else {
|
410
|
+
|
411
|
+
return false;
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
}
|
358
420
|
|
359
421
|
|
360
422
|
|
361
423
|
```
|
362
424
|
|
363
|
-
|
425
|
+
StudentEditServlet.java
|
426
|
+
|
364
|
-
|
427
|
+
```ここに言語を入力
|
428
|
+
|
429
|
+
//import省略
|
430
|
+
|
431
|
+
public class StudentEditServlet extends HttpServlet{
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
Connection conn = null;
|
436
|
+
|
437
|
+
PreparedStatement ps = null;
|
438
|
+
|
439
|
+
ResultSet result = null;]
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
String user = "suser";
|
444
|
+
|
445
|
+
String password = "spass";
|
446
|
+
|
447
|
+
String url = "jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false";
|
448
|
+
|
449
|
+
|
450
|
+
|
365
|
-
|
451
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
|
452
|
+
|
366
|
-
|
453
|
+
request.setCharacterEncoding("UTF-8");
|
454
|
+
|
455
|
+
|
456
|
+
|
367
|
-
|
457
|
+
String status = "Success!";
|
368
|
-
|
458
|
+
|
369
|
-
|
459
|
+
StudentBeans student = new StudentBeans(request);
|
460
|
+
|
461
|
+
|
462
|
+
|
370
|
-
|
463
|
+
String student_id = request.getParameter("student_id");
|
464
|
+
|
371
|
-
|
465
|
+
String student_name = request.getParameter("student_name");
|
466
|
+
|
372
|
-
|
467
|
+
String student_furigana = request.getParameter("student_furigana");
|
468
|
+
|
469
|
+
String school_year = request.getParameter("school_year");
|
470
|
+
|
471
|
+
String school_class = request.getParameter("school_class");
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
boolean exist_regist_err = false;
|
476
|
+
|
477
|
+
String s_id_err = "生徒IDは半角数字で入力してください";
|
478
|
+
|
479
|
+
String name_err = "名前が空白です";
|
480
|
+
|
481
|
+
String furigana_err = "フリガナが空白です";
|
482
|
+
|
483
|
+
String year_err = "学年が空白です";
|
484
|
+
|
485
|
+
String class_err = "クラスが空白です";
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
try {
|
490
|
+
|
491
|
+
//エラーチェック
|
492
|
+
|
493
|
+
if(!student_id.equals("") && !StudentBeans.isNumber(student_id)) {
|
494
|
+
|
495
|
+
request.setAttribute("s_id_err", s_id_err);
|
496
|
+
|
497
|
+
exist_regist_err = true;
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
if(student.getStudent_name().equals("")) {
|
502
|
+
|
503
|
+
request.setAttribute("name_err", name_err);
|
504
|
+
|
505
|
+
exist_regist_err = true;
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
if(student.getStudent_furigana().equals("")) {
|
510
|
+
|
511
|
+
request.setAttribute("furigana_err", furigana_err);
|
512
|
+
|
513
|
+
exist_regist_err = true;
|
514
|
+
|
515
|
+
} else if(!student.isFurigana(student.getStudent_furigana())) {
|
516
|
+
|
517
|
+
furigana_err = "フリガナは全角カナで入力してください";
|
518
|
+
|
519
|
+
request.setAttribute("furigana_err", furigana_err);
|
520
|
+
|
521
|
+
exist_regist_err = true;
|
522
|
+
|
523
|
+
}
|
524
|
+
|
525
|
+
if(student.getSchool_year().equals("")) {
|
526
|
+
|
527
|
+
request.setAttribute("year_err", year_err);
|
528
|
+
|
529
|
+
exist_regist_err = true;
|
530
|
+
|
531
|
+
} else if(!student.isNumber(student.getSchool_year())) {
|
532
|
+
|
533
|
+
year_err = "学年は半角数字で入力してください";
|
534
|
+
|
535
|
+
request.setAttribute("year_err", year_err);
|
536
|
+
|
537
|
+
exist_regist_err = true;
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
if(student.getSchool_class().equals("")) {
|
542
|
+
|
543
|
+
request.setAttribute("class_err", class_err);
|
544
|
+
|
545
|
+
exist_regist_err = true;
|
546
|
+
|
547
|
+
} else if(!student.isNumber(student.getSchool_class())) {
|
548
|
+
|
549
|
+
class_err = "クラスは半角数字で入力してください";
|
550
|
+
|
551
|
+
request.setAttribute("class_err", class_err);
|
552
|
+
|
553
|
+
exist_regist_err = true;
|
554
|
+
|
555
|
+
}
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
if(exist_regist_err) {
|
560
|
+
|
561
|
+
request.setAttribute("student_id_regist", student_id);
|
562
|
+
|
563
|
+
request.setAttribute("student_name_regist", student_name);
|
564
|
+
|
565
|
+
request.setAttribute("student_furigana_regist", student_furigana);
|
566
|
+
|
567
|
+
request.setAttribute("school_year_regist", school_year);
|
568
|
+
|
569
|
+
request.setAttribute("school_class_regist", school_class);
|
570
|
+
|
373
|
-
|
571
|
+
status = "Failed...";
|
572
|
+
|
573
|
+
} else {
|
574
|
+
|
575
|
+
if(student.registData() == false) {
|
576
|
+
|
577
|
+
status = "Failed...";
|
578
|
+
|
579
|
+
}
|
580
|
+
|
581
|
+
}
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
request.setAttribute("exist_regist_err", exist_regist_err);
|
586
|
+
|
587
|
+
request.setAttribute("status", status);
|
588
|
+
|
589
|
+
request.getRequestDispatcher("student.jsp").forward(request, response);
|
590
|
+
|
591
|
+
|
592
|
+
|
593
|
+
} catch(Exception e) {
|
594
|
+
|
595
|
+
e.printStackTrace();
|
596
|
+
|
597
|
+
} finally {
|
598
|
+
|
599
|
+
try {
|
600
|
+
|
601
|
+
conn.close();
|
602
|
+
|
603
|
+
} catch (SQLException e) {
|
604
|
+
|
605
|
+
System.out.println("MySQLのクローズに失敗しました。");
|
606
|
+
|
607
|
+
}
|
608
|
+
|
609
|
+
}
|
610
|
+
|
611
|
+
|
612
|
+
|
613
|
+
}
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
}
|
618
|
+
|
619
|
+
|
620
|
+
|
621
|
+
```
|