質問編集履歴

3

解決方法を追記いたしました

2019/07/11 02:45

投稿

asakuta
asakuta

スコア100

test CHANGED
File without changes
test CHANGED
@@ -237,3 +237,221 @@
237
237
 
238
238
 
239
239
  AjaxでServletと連携する場合setAttributeはしない方がよろしいのでしょうか?
240
+
241
+
242
+
243
+ ### 追記
244
+
245
+ mts10806さんの指摘どうりJSONデータを使用することで解決できたため以下にコードを載せます
246
+
247
+ ```JavaScript
248
+
249
+ $ .ajax({ type : "POST",
250
+
251
+ url : "Test",
252
+
253
+ dataType : "json",
254
+
255
+ success : function(data) {
256
+
257
+
258
+
259
+ $("#a")
260
+
261
+
262
+
263
+ for (j = 0; j < data.emp_list.length; j++) {
264
+
265
+ $("#abcd")
266
+
267
+ .append(
268
+
269
+ "<tr><td>"
270
+
271
+ + data.emp_list[j].emp_id
272
+
273
+ +"</td><td>"
274
+
275
+ + data.emp_list[j].emp_name
276
+
277
+ + "</td></tr>")
278
+
279
+ }
280
+
281
+
282
+
283
+ },
284
+
285
+ ```
286
+
287
+ ```Java
288
+
289
+ package todo.controller;
290
+
291
+
292
+
293
+ import java.io.IOException;
294
+
295
+ import java.io.PrintWriter;
296
+
297
+ import java.sql.Connection;
298
+
299
+ import java.sql.PreparedStatement;
300
+
301
+ import java.sql.ResultSet;
302
+
303
+ import java.sql.SQLException;
304
+
305
+ import java.util.ArrayList;
306
+
307
+ import java.util.HashMap;
308
+
309
+ import java.util.Map;
310
+
311
+
312
+
313
+ import javax.servlet.RequestDispatcher;
314
+
315
+ import javax.servlet.ServletException;
316
+
317
+ import javax.servlet.annotation.WebServlet;
318
+
319
+ import javax.servlet.http.HttpServlet;
320
+
321
+ import javax.servlet.http.HttpServletRequest;
322
+
323
+ import javax.servlet.http.HttpServletResponse;
324
+
325
+
326
+
327
+ import org.apache.jasper.tagplugins.jstl.core.Out;
328
+
329
+
330
+
331
+ import com.fasterxml.jackson.databind.ObjectMapper;
332
+
333
+ import com.fasterxml.jackson.core.JsonProcessingException;
334
+
335
+ import com.fasterxml.jackson.databind.ObjectMapper;
336
+
337
+ import todo.controller.db;
338
+
339
+
340
+
341
+ /**
342
+
343
+ * Servlet implementation class HelloServlet
344
+
345
+ */
346
+
347
+ @WebServlet("/Test")
348
+
349
+ public class Test extends HttpServlet {
350
+
351
+
352
+
353
+ static Connection connection = null;
354
+
355
+ static PreparedStatement preparedStatement = null;
356
+
357
+ static ResultSet resultSet = null;
358
+
359
+ ArrayList<Object> AllSearch_JSON = new ArrayList<Object>();
360
+
361
+ private static final long serialVersionUID = 1L;
362
+
363
+ String all_json = null;
364
+
365
+ /**
366
+
367
+ * @see HttpServlet#HttpServlet()
368
+
369
+ */
370
+
371
+ public Test() {
372
+
373
+ super();
374
+
375
+ }
376
+
377
+
378
+
379
+ /**
380
+
381
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
382
+
383
+ */
384
+
385
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
386
+
387
+ doPost(request, response);
388
+
389
+ }
390
+
391
+
392
+
393
+ /**
394
+
395
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
396
+
397
+ */
398
+
399
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
400
+
401
+ throws ServletException, IOException {
402
+
403
+ try {
404
+
405
+ ObjectMapper mapper = new ObjectMapper();
406
+
407
+
408
+
409
+ String json = mapper.writeValueAsString(emp.EmployeeList());
410
+
411
+ response.setContentType("application/json;charset=UTF-8");
412
+
413
+ PrintWriter pw = response.getWriter();
414
+
415
+ pw.print("{"+"\"emp_list\":"+json+"}");
416
+
417
+ } catch (Exception e) {
418
+
419
+
420
+
421
+ }
422
+
423
+ }
424
+
425
+ }
426
+
427
+ ```
428
+
429
+ ```json
430
+
431
+ //作成したJSONデータ
432
+
433
+ {
434
+
435
+ "emp_list": [{
436
+
437
+ "emp_id": 4,
438
+
439
+ "emp_name": "田中太郎"
440
+
441
+ }, {
442
+
443
+ "emp_id": 5,
444
+
445
+ "emp_name": "田中次郎"
446
+
447
+ }, {
448
+
449
+ "emp_id": 7,
450
+
451
+ "emp_name": "田中三郎"
452
+
453
+ }]
454
+
455
+ }
456
+
457
+ ```

2

classが同じだったため変更

2019/07/11 02:45

投稿

asakuta
asakuta

スコア100

test CHANGED
File without changes
test CHANGED
@@ -158,7 +158,7 @@
158
158
 
159
159
  //Ajaxと連携するServlet
160
160
 
161
- public class HelloServlet extends HttpServlet {
161
+ public class HelloServlet2 extends HttpServlet {
162
162
 
163
163
 
164
164
 

1

加筆しました

2019/07/09 10:13

投稿

asakuta
asakuta

スコア100

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,8 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- AjaxでServletに通信を行いsetAttributeの内容をsuccess
3
+ AjaxでServletに通信を行いsetAttributeの内容を描画したいのですがエラーが出て困っています。
4
+
5
+ エラー理由は、最初に読み込むjspの中にクリック時に読み込むsetAttributeが入っているためだと推測されます。どのように改修すれば良いのか教えていただけませんか?
4
6
 
5
7
 
6
8
 
@@ -10,7 +12,11 @@
10
12
 
11
13
  ```
12
14
 
15
+ org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/view/index.jsp] at line [81]
16
+
13
- エラーメッセージ
17
+ Caused by: java.lang.NullPointerException
18
+
19
+
14
20
 
15
21
  ```
16
22
 
@@ -22,18 +28,12 @@
22
28
 
23
29
  ```java
24
30
 
25
- package todo.controller;
26
-
27
-
28
-
29
31
  /**
30
32
 
31
33
  * 最初に呼び出される
32
34
 
33
35
  */
34
36
 
35
- @WebServlet("/HelloServlet")
36
-
37
37
  public class HelloServlet extends HttpServlet {
38
38
 
39
39
 
@@ -114,85 +114,115 @@
114
114
 
115
115
  ```jsp
116
116
 
117
+ //DBのAの値を描画
118
+
119
+ <td id=abcd><%request.getAttribute("1")%><td>
120
+
121
+
122
+
117
123
  $(function() {
118
124
 
119
- $("#AllSearch")
120
-
121
- .click(
122
-
123
- function() {
124
-
125
- // Ajax通信を開始する
126
-
127
- $
128
-
129
- .ajax({
130
-
131
- type : "POST",
132
-
133
- url : "Test",
134
-
135
- data : {
136
-
137
- param1 : "AllSerch",
138
-
139
- },
140
-
141
- success : function() {
142
-
143
- console.log("成功");
144
-
145
- $("#abcd")
146
-
147
- .empty()
148
-
149
- .append(
150
-
151
- <%int Employee_Size = (int) (request.getAttribute("All_Employee_Size"));
152
-
153
- for (int i = 0; i < Employee_Size; i++) {
154
-
155
- out.print("$(\"#abcd\").append(\"");
156
-
157
- out.print("<tr>");
158
-
159
- out.print("<td>" + request.getAttribute("All_emp_id" + i) + "</td>");
160
-
161
- out.print("<td>" + request.getAttribute("All_emp_name" + i) + "</td>");
162
-
163
- out.print("<td>" + request.getAttribute("All_gender" + i) + "</td>");
164
-
165
- out.print("<td>" + request.getAttribute("All_birthday" + i) + "</td>");
166
-
167
- out.print("<td>" + request.getAttribute("All_dept_name" + i) + "</td>");
168
-
169
- out.print("<tr>");
170
-
171
- out.print("\");");
172
-
173
- }%>
174
-
175
- $("#abcd")
176
-
177
- .append(
178
-
179
- "<tr><td>amerika</td></tr>");
180
-
181
-
182
-
183
- },
184
-
185
- error : function() {
186
-
187
- console.log("失敗");
188
-
189
- }
190
-
191
- })
192
-
193
- });
194
-
195
- });
125
+ $("#Search").click(function() {
126
+
127
+ // Ajax通信を開始する
128
+
129
+ $.ajax({
130
+
131
+ type : "POST",
132
+
133
+ url : "Test",
134
+
135
+ success : function() {
136
+
137
+ console.log("成功");
138
+
139
+ //DBのAの値を消してBの値に置き換える*NullPointerExceptionエラーが出る
140
+
141
+ $("#abcd").empty().append(<%request.getAttribute("2")%>)
142
+
143
+ },
144
+
145
+ error : function() {
146
+
147
+ console.log("失敗");}
148
+
149
+ })
150
+
151
+ });
152
+
153
+ });
154
+
155
+ ```
156
+
157
+ ```Java
158
+
159
+ //Ajaxと連携するServlet
160
+
161
+ public class HelloServlet extends HttpServlet {
162
+
163
+
164
+
165
+ private static final long serialVersionUID = 1L;
166
+
167
+
168
+
169
+ /**
170
+
171
+ * @see HttpServlet#HttpServlet()
172
+
173
+ */
174
+
175
+ public HelloServlet() {
176
+
177
+ super();
178
+
179
+ }
180
+
181
+
182
+
183
+ /**
184
+
185
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
186
+
187
+ */
188
+
189
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
190
+
191
+ doPost(request, response);
192
+
193
+ }
194
+
195
+
196
+
197
+ /**
198
+
199
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
200
+
201
+ */
202
+
203
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
204
+
205
+ throws ServletException, IOException {
206
+
207
+ try {
208
+
209
+ //DBに接続する BはDBの値
210
+
211
+        request.setAttribute("2","B")
212
+
213
+
214
+
215
+ } catch (Exception e) {
216
+
217
+ e.printStackTrace();
218
+
219
+ }
220
+
221
+ }
222
+
223
+
224
+
225
+ }
196
226
 
197
227
  ```
198
228
 
@@ -202,14 +232,8 @@
202
232
 
203
233
  ### 試したこと
204
234
 
205
-
206
-
207
- ここ問題してしたことを記載てください
235
+ Ajaxと通信するServletのsetAttribute内変数を最初読み込むServletと同じにしてエラーを表示ないようにしましが結局nullが出ており読み込めませんで
208
-
209
-
210
-
236
+
237
+
238
+
211
- ### 補足情報(FW/ツールバージョンなど)
239
+ AjaxでServletと連携する場合setAttributeはしない方がよろしいでしょうか?
212
-
213
-
214
-
215
- ここにより詳細な情報を記載してください。