質問編集履歴

1

作成したい画面のJSPを追記 書き直したjavaコードを貼り付け

2017/08/17 09:07

投稿

cayribaka
cayribaka

スコア19

test CHANGED
File without changes
test CHANGED
@@ -4,51 +4,15 @@
4
4
 
5
5
  言語はJavaです。
6
6
 
7
-
8
-
9
7
  ユーザーの名前や身長、体重を登録する画面を作成しているのですが、DBのデータを更新することができません。
10
8
 
11
-
12
-
13
9
  DBとの接続はDAOを使っています。
14
10
 
15
-
16
-
17
- コードのどこを変えればよいのかわからず、詰まっております。
18
-
19
- 助けてください。
20
-
21
11
  ```Java
22
12
 
23
13
  package servlet;
24
14
 
25
-
26
-
27
- import java.io.IOException;
28
-
29
-
30
-
31
- import javax.servlet.RequestDispatcher;
32
-
33
- import javax.servlet.ServletException;
34
-
35
- import javax.servlet.annotation.WebServlet;
36
-
37
- import javax.servlet.http.HttpServlet;
38
-
39
- import javax.servlet.http.HttpServletRequest;
40
-
41
- import javax.servlet.http.HttpServletResponse;
42
-
43
- import javax.servlet.http.HttpSession;
44
-
45
-
46
-
47
- import model.SetUpLogic;
15
+ //import文省略
48
-
49
- import model.User;
50
-
51
-
52
16
 
53
17
  @WebServlet("/SetUp")
54
18
 
@@ -62,297 +26,311 @@
62
26
 
63
27
  HttpSession session = request.getSession();
64
28
 
65
- //String userName ;
66
-
67
- Double height ;
68
-
69
- Double targetWeight;
70
-
71
- StringBuffer urlBuffer = request.getRequestURL();
29
+ //StringBuffer urlBuffer = request.getRequestURL();
72
-
30
+
73
- String url = urlBuffer.append("?").append(request.getQueryString()).toString();
31
+ //String url = urlBuffer.toString();
74
32
 
75
33
  User loginUser = (User) session.getAttribute("loginUser");
76
34
 
77
- int indexOfParam = url.indexOf("=");
78
-
79
-
80
-
81
- if(loginUser != null) {
82
-
83
- if(url.indexOf("?") != -1) { // URLにパラメータが含まれている時
84
-
85
- String userName = request.getParameter("userName");
86
-
87
- String Height = request.getParameter("Height");
88
-
89
- String strTargetWeight = request.getParameter("strTargetWeight");
90
-
91
-
92
-
93
- //それぞれのパラメータの値を上の3つの変数に代入
94
-
95
- String userData="SetUp?userName=" + userName + "&height=" + Height + "&targetWeight=" + strTargetWeight;
96
-
97
- SetUpLogic sul=new SetUpLogic();
98
-
99
- String errorMsg = "";
100
-
101
- try {
102
-
103
- boolean judge = false; // 不正な入力値ではないかどうか
104
-
105
- boolean noInputText = false; // 未入力のテキストボックスが存在するかどうか
106
-
107
- request.setCharacterEncoding("utf-8");
108
-
109
- if((url.length() == 0)) {
110
-
111
- url = loginUser.getUserName();
112
-
113
- noInputText = true;
35
+
36
+
37
+ //データの読み込み
38
+
39
+ if(loginUser != null){
40
+
41
+ request.setAttribute("userName",loginUser.getUserName());
42
+
43
+ //System.out.println("userName");
44
+
45
+ request.setAttribute("height", loginUser.getHeight() != 0 ? String.format("%.2f", loginUser.getHeight()) : "");
46
+
47
+ //System.out.println("height");
48
+
49
+ request.setAttribute("targetWeight", loginUser.getHeight() != 0 ? String.format("%.2f", loginUser.getTargetWeight()) : "");
50
+
51
+ //System.out.println("targetWeight");
52
+
53
+
54
+
55
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
56
+
57
+ requestDispatcher.forward(request, response);
58
+
59
+ }
60
+
61
+ else {
62
+
63
+ response.sendRedirect("/bodyMassIndex/");
64
+
65
+ }
66
+
67
+ }
68
+
69
+
70
+
71
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
72
+
73
+ //データの書き込み(更新)名前・身長・目標体重を送信する。
74
+
75
+
76
+
77
+ String errorMsg = "";
78
+
79
+ //入力された値のチェック(name)
80
+
81
+ try {
82
+
83
+ boolean judge = false; // 不正な入力値ではないかどうか
84
+
85
+ boolean noInputText = false; // 未入力のテキストボックスが存在するかどうか
86
+
87
+ request.setCharacterEncoding("utf-8");
88
+
89
+ HttpSession session = request.getSession();
90
+
91
+ User loginUser = (User) session.getAttribute("loginUser");
92
+
93
+ //フォームに入力されたデータの取得
94
+
95
+ String userName =request.getParameter("userName"); /* 入力された名前を代入 */
96
+
97
+ if((userName.length() == 0)) {
98
+
99
+ userName = loginUser.getUserName();
100
+
101
+ noInputText = true;
102
+
103
+ }
104
+
105
+ else if (userName.length() > 64) {
106
+
107
+ userName = loginUser.getUserName();
108
+
109
+ errorMsg = "名前は64文字以内で入力してください";
110
+
111
+ throw new IllegalArgumentException();
112
+
113
+ }
114
+
115
+ else {
116
+
117
+ judge = true;
118
+
119
+ }
120
+
121
+
122
+
123
+ //フォームに入力されたheightの値を取得する
124
+
125
+ String height = request.getParameter("height");
126
+
127
+ // 以下、入力された値の桁数チェック
128
+
129
+
130
+
131
+ int seisuKeta = 0;
132
+
133
+ int shosuKeta = 0;
134
+
135
+ int conmaIndex = height.indexOf(".");
136
+
137
+ if(conmaIndex != -1) {
138
+
139
+ seisuKeta = height.substring(0, conmaIndex).length();
140
+
141
+ shosuKeta = height.substring(conmaIndex + 1).length();
142
+
143
+ }
144
+
145
+ else {
146
+
147
+ seisuKeta = height.length();
148
+
149
+ }
150
+
151
+ if(3 < seisuKeta || 2 < shosuKeta) {
152
+
153
+ errorMsg = "身長の整数桁は3桁、小数点以下の桁は2桁までで入力し直してください。";
154
+
155
+ throw new IllegalArgumentException();
156
+
157
+ }
158
+
159
+
160
+
161
+ // 桁数チェック終わり
162
+
163
+
164
+
165
+ if(height.length() == 0) {
166
+
167
+ //身長はdouble型で格納されなければならないので、現在String型のheightをdouble型のdheightに変換する。
168
+
169
+ double dheight = Double.parseDouble(height);
170
+
171
+ dheight = loginUser.getHeight();
172
+
173
+ noInputText = true;
174
+
175
+ }
176
+
177
+ else {
178
+
179
+ double dheight = Double.parseDouble(height);
180
+
181
+ dheight = loginUser.getHeight();
182
+
183
+ errorMsg = "現在の身長は、数字で入力してください。";
184
+
185
+ if(dheight <= 0 || height.indexOf("Infinity") != -1 || height.indexOf("NaN") != -1) {
186
+
187
+ errorMsg = "身長は正の実数で入力してください。";
188
+
189
+ throw new IllegalArgumentException();
190
+
191
+ }
192
+
193
+ judge = true;
194
+
195
+ }
196
+
197
+
198
+
199
+ //フォームに入力されたtargetWeightの値を取得する。
200
+
201
+ String targetWeight = request.getParameter("targetWeight");
202
+
203
+ //入力された値の桁数チェック
204
+
205
+
206
+
207
+ int seisuKeta2 = 0;
208
+
209
+ int shosuKeta2=0;
210
+
211
+ int conmaIndex2 = targetWeight.indexOf(".");
212
+
213
+ if(conmaIndex2 !=-1){
214
+
215
+ seisuKeta2=targetWeight.substring(0, conmaIndex2).length();
216
+
217
+ shosuKeta2=targetWeight.substring(conmaIndex2 + 1).length();
218
+
219
+ }
220
+
221
+ else{
222
+
223
+ seisuKeta2=targetWeight.length();
224
+
225
+ }
226
+
227
+ if(3 < seisuKeta2 || 2 < shosuKeta2) {
228
+
229
+ errorMsg = "目標体重の整数桁は3桁、小数点以下の桁は2桁までで入力し直してください。";
230
+
231
+ throw new IllegalArgumentException();
232
+
233
+ }
234
+
235
+ //桁数チェック終わり
236
+
237
+
238
+
239
+ if(targetWeight.length() == 0) {
240
+
241
+ //目標体重はdouble型で格納されなければならないので、現在String型のtargetWeightをdouble型のdtargetWeightに変換する。
242
+
243
+ double dtargetWeight = Double.parseDouble(targetWeight);
244
+
245
+ dtargetWeight = loginUser.getTargetWeight();
246
+
247
+ noInputText = true;
248
+
249
+ }
250
+
251
+ else {
252
+
253
+ errorMsg = "目標体重は、数字で入力してください。";
254
+
255
+ //目標体重はdouble型で格納されなければならないので、現在String型のtargetWeightをdouble型のdtargetWeightに変換する。
256
+
257
+ double dtargetWeight =Double.parseDouble(targetWeight);/* 入力された目標体重を代入 */
258
+
259
+ if(dtargetWeight <= 0 || targetWeight.indexOf("Infinity") != -1 || targetWeight.indexOf("NaN") != -1) {
260
+
261
+ errorMsg = "目標体重は正の実数で入力してください。";
262
+
263
+ throw new IllegalArgumentException();
264
+
265
+ }
266
+
267
+ judge = true;
268
+
269
+ }
270
+
271
+ request.setAttribute("userName", "");
272
+
273
+ request.setAttribute("height", "");
274
+
275
+ request.setAttribute("targetWeight", "");
276
+
277
+ if(judge) {
278
+
279
+ if(noInputText) {
280
+
281
+ request.setAttribute("errorMessage", "情報を入力して下さい");
282
+
283
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
284
+
285
+ requestDispatcher.forward(request, response);
286
+
287
+ }
288
+
289
+
290
+
291
+ //データの書き込み(更新)名前・身長・目標体重を送信する。
292
+
293
+ else {
294
+
295
+ SetUpLogic sul=new SetUpLogic(); /* SetUpLogicのインスタンス化 */
296
+
297
+ double dheight = Double.parseDouble(height);
298
+
299
+ dheight = loginUser.getHeight();
300
+
301
+ double dtargetWeight = Double.parseDouble(targetWeight);
302
+
303
+ dtargetWeight = loginUser.getTargetWeight();
304
+
305
+ boolean isUpdate =sul.execute(userName, dheight, dtargetWeight, loginUser); /* インスタンス化した変数でexecuteメソッドを呼ぶ */;
306
+
307
+ if(isUpdate) {
308
+
309
+ request.setAttribute("userName",loginUser.getUserName());
310
+
311
+ request.setAttribute("height", String.format("%.2f", loginUser.getHeight()));
312
+
313
+ request.setAttribute("targetWeight",String.format("%.2f", loginUser.getTargetWeight()));
314
+
315
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
316
+
317
+ requestDispatcher.forward(request, response);
114
318
 
115
319
  }
116
320
 
117
- else if (url.length() > 64) {
321
+ else {
118
-
119
- url = loginUser.getUserName();
322
+
120
-
121
- errorMsg = "名前は64文字以内で入力してください";
323
+ response.sendRedirect("SetUp");/* updateに失敗したら、ここに来る */
122
-
123
- throw new IllegalArgumentException();
124
324
 
125
325
  }
126
326
 
127
- else {
128
-
129
- judge = true;
130
-
131
- }
132
-
133
-
134
-
135
- String Heights = request.getParameter("height");
136
-
137
- //Double Sincyo;
138
-
139
- // 以下、入力された値の桁数チェック
140
-
141
-
142
-
143
- int seisuKeta = 0;
144
-
145
- int shosuKeta = 0;
146
-
147
- int conmaIndex = Heights.indexOf(".");
148
-
149
- if(conmaIndex != -1) {
150
-
151
- seisuKeta = Heights.substring(0, conmaIndex).length();
152
-
153
- shosuKeta = Heights.substring(conmaIndex + 1).length();
154
-
155
- }
156
-
157
- else {
158
-
159
- seisuKeta =Heights.length();
160
-
161
- }
162
-
163
- if(3 < seisuKeta || 2 < shosuKeta) {
164
-
165
- errorMsg = "身長の整数桁は3桁、小数点以下の桁は2桁までで入力し直してください。";
166
-
167
- throw new IllegalArgumentException();
168
-
169
- }
170
-
171
-
172
-
173
- // 桁数チェック終わり
174
-
175
-
176
-
177
- if(Heights.length() == 0) {
178
-
179
- height = loginUser.getHeight();
180
-
181
- noInputText = true;
182
-
183
- }
184
-
185
- else {
186
-
187
- errorMsg = "現在の身長は、数字で入力してください。";
188
-
189
- height =Double.parseDouble(url);
190
-
191
- if(height <= 0 || Heights.indexOf("Infinity") != -1 || url.indexOf("NaN") != -1) {
192
-
193
- errorMsg = "身長は正の実数で入力してください。";
194
-
195
- throw new IllegalArgumentException();
196
-
197
- }
198
-
199
- judge = true;
200
-
201
- }
202
-
203
- String TargetWeight = request.getParameter("targetWeight");
204
-
205
- double targetWeights;
206
-
207
- //入力された値の桁数チェック
208
-
209
-
210
-
211
- int seisuKeta2 = 0;
212
-
213
- int shosuKeta2=0;
214
-
215
- int conmaIndex2 = TargetWeight.indexOf(".");
216
-
217
- if(conmaIndex2 !=-1){
218
-
219
- seisuKeta2=TargetWeight.substring(0, conmaIndex2).length();
220
-
221
- shosuKeta2=TargetWeight.substring(conmaIndex2 + 1).length();
222
-
223
- }
224
-
225
- else{
226
-
227
- seisuKeta2=TargetWeight.length();
228
-
229
- }
230
-
231
- if(3 < seisuKeta2 || 2 < shosuKeta2) {
232
-
233
- errorMsg = "目標体重の整数桁は3桁、小数点以下の桁は2桁までで入力し直してください。";
234
-
235
- throw new IllegalArgumentException();
236
-
237
- }
238
-
239
- //桁数チェック終わり
240
-
241
-
242
-
243
- if(TargetWeight.length() == 0) {
244
-
245
- targetWeight = loginUser.getTargetWeight();
246
-
247
- noInputText = true;
248
-
249
- }
250
-
251
- else {
252
-
253
- errorMsg = "目標体重は、数字で入力してください。";
254
-
255
- targetWeight =Double.parseDouble(url);
256
-
257
- if(targetWeight <= 0 || TargetWeight.indexOf("Infinity") != -1 || TargetWeight.indexOf("NaN") != -1) {
258
-
259
- errorMsg = "目標体重は正の実数で入力してください。";
260
-
261
- throw new IllegalArgumentException();
262
-
263
- }
264
-
265
- judge = true;
266
-
267
- }
268
-
269
- request.setAttribute("userName", "");
270
-
271
- request.setAttribute("height", "");
272
-
273
- request.setAttribute("targetWeight", "");
274
-
275
- if(judge) {
276
-
277
- if(noInputText) {
278
-
279
- request.setAttribute("errorMessage", "情報を入力して下さい");
280
-
281
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
282
-
283
- requestDispatcher.forward(request, response);
284
-
285
- }
286
-
287
- else {
288
-
289
- //SetUpLogic SUL=new SetUpLogic();
290
-
291
- boolean isUpdate =sul.execute(url, height, targetWeight, loginUser);
292
-
293
- if(isUpdate) {
294
-
295
- request.setAttribute("userName",loginUser.getUserName());
296
-
297
- request.setAttribute("height", String.format("%.2f", loginUser.getHeight()));
298
-
299
- request.setAttribute("targetWeight",String.format("%.2f", loginUser.getTargetWeight()));
300
-
301
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
302
-
303
- requestDispatcher.forward(request, response);
304
-
305
- }
306
-
307
- else {
308
-
309
- response.sendRedirect("SetUp");
310
-
311
- }
312
-
313
- }
314
-
315
- }
316
-
317
- else {
318
-
319
- request.setAttribute("errorMessage", "情報を入力して下さい");
320
-
321
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
322
-
323
- requestDispatcher.forward(request, response);
324
-
325
- }
326
-
327
- } catch (IllegalArgumentException e) {
328
-
329
- request.setAttribute("userName", "");
330
-
331
- request.setAttribute("height", "");
332
-
333
- request.setAttribute("targetWeight", "");
334
-
335
- request.setAttribute("errorMessage", errorMsg);
336
-
337
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
338
-
339
- requestDispatcher.forward(request, response);
340
-
341
327
  }
342
328
 
343
- }
329
+ }
344
-
345
-
346
-
347
-
348
330
 
349
331
  else {
350
332
 
351
- request.setAttribute("userName", "");
333
+ request.setAttribute("errorMessage", "情報を入力して下さい");
352
-
353
- request.setAttribute("height", "");
354
-
355
- request.setAttribute("targetWeight", "");
356
334
 
357
335
  RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
358
336
 
@@ -360,36 +338,148 @@
360
338
 
361
339
  }
362
340
 
363
- }
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
- //System.out.println(userName);
341
+ } catch (IllegalArgumentException e) {
374
-
375
- //System.out.println(height);
342
+
376
-
377
- //System.out.println(targetWeight);
378
-
379
-
380
-
381
- else { // URLにパラメータが含まれていない時
382
-
383
- request.setAttribute("userName",loginUser.getUserName());
343
+ request.setAttribute("userName", "");
344
+
384
-
345
+ request.setAttribute("height", "");
346
+
385
- request.setAttribute("height", loginUser.getHeight() != 0 ? String.format("%.2f", loginUser.getHeight()) : "");
347
+ request.setAttribute("targetWeight", "");
386
-
348
+
387
- request.setAttribute("targetWeight", loginUser.getHeight() != 0 ? String.format("%.2f", loginUser.getTargetWeight()) : "");
349
+ request.setAttribute("errorMessage", errorMsg);
388
350
 
389
351
  RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
390
352
 
391
353
  requestDispatcher.forward(request, response);
392
354
 
393
- }
355
+ }
356
+
357
+ }
358
+
359
+ else {
360
+
361
+ request.setAttribute("userName", "");
362
+
363
+ request.setAttribute("height", "");
364
+
365
+ request.setAttribute("targetWeight", "");
366
+
367
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/setUp.jsp");
368
+
369
+ requestDispatcher.forward(request, response);
370
+
371
+ }
372
+
373
+ }
374
+
375
+
394
376
 
395
377
  ```
378
+
379
+ ```JSP
380
+
381
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
382
+
383
+ pageEncoding="UTF-8"%>
384
+
385
+ <!DOCTYPE html">
386
+
387
+ <html>
388
+
389
+ <head>
390
+
391
+ <link rel="stylesheet" type="text/css" href="css/menu.css"/>
392
+
393
+ <link rel="stylesheet" type="text/css" href="css/setUp.css?20170811"/>
394
+
395
+ <meta charset="UTF-8">
396
+
397
+ <title>体重管理アプリケーション</title>
398
+
399
+ <script src="js/lib/jquery-3.2.1.min.js"></script>
400
+
401
+ <script src="js/menu.js"></script>
402
+
403
+ <script type="text/javascript">
404
+
405
+ function disp(userId){
406
+
407
+ myRet = confirm("情報を更新してよろしいですか?");
408
+
409
+
410
+
411
+ if ( myRet == true ){
412
+
413
+ var userName = document.getElementById('text1').value;
414
+
415
+ var height = document.getElementById('text2').value;
416
+
417
+ var targetWeight = document.getElementById('text3').value;
418
+
419
+
420
+
421
+ location.href= ' /bmi/SetUp?userName=' + userName + '&height=' + height + '&targetWeight=' + targetWeight;
422
+
423
+ }else{
424
+
425
+ alert("キャンセルされました");
426
+
427
+
428
+
429
+ }
430
+
431
+ }
432
+
433
+ </script>
434
+
435
+
436
+
437
+ </head>
438
+
439
+ <body onload="selectColorChange(4);">
440
+
441
+ <jsp:include page="menu.jsp"/>
442
+
443
+ <div id="div1">
444
+
445
+ <h1>設定情報</h1>
446
+
447
+ <p id="errorMessage"><%= request.getAttribute("errorMessage") != null ? request.getAttribute("errorMessage") : "" %></p>
448
+
449
+ <input id="text1" type="text" name="userName" value="<%= request.getAttribute("userName") %>" placeholder="お名前"/>
450
+
451
+ <br/>
452
+
453
+ <input id="text2" type="text" name="height" value="<%= request.getAttribute("height") %>" placeholder="現在の身長"/>cm
454
+
455
+ <br/>
456
+
457
+ <input id="text3" type="text" name="targetWeight" value="<%= request.getAttribute("targetWeight") %>" placeholder="目標体重"/>kg
458
+
459
+ <br/>
460
+
461
+ <input id="button1" type="submit" value="保存" onclick="disp()"/>
462
+
463
+ </div>
464
+
465
+ </body>
466
+
467
+ </html>
468
+
469
+ ```
470
+
471
+ 改めてコードを書き直しました。
472
+
473
+ エラー java.lang.NullPointerException servlet.SetUp.doGet(SetUp.java:63) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
474
+
475
+ は消えました。
476
+
477
+
478
+
479
+ 値の受け取りはできますが、doPost()からの値の引き渡しができていないです。
480
+
481
+ ブラウザの入力のフォームに値を入力して、保存ボタンを押すと、ブラウザのアドレスバーにはこのように
482
+
483
+ http://localhost:xxxx/bmi/SetUp?userName=田中&height=169.00&targetWeight=50.00 値は入っています。
484
+
485
+ 結局コードが長いです。すみません。