質問編集履歴
3
解決方法を追記いたしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -117,4 +117,113 @@
|
|
117
117
|
### 試したこと
|
118
118
|
Ajaxと通信するServletのsetAttribute内変数を最初に読み込むServletと同じにしてエラーを表示しないようにしましたが結局nullが出ており読み込めませんでした。
|
119
119
|
|
120
|
-
AjaxでServletと連携する場合setAttributeはしない方がよろしいのでしょうか?
|
120
|
+
AjaxでServletと連携する場合setAttributeはしない方がよろしいのでしょうか?
|
121
|
+
|
122
|
+
### 追記
|
123
|
+
mts10806さんの指摘どうりJSONデータを使用することで解決できたため以下にコードを載せます
|
124
|
+
```JavaScript
|
125
|
+
$ .ajax({ type : "POST",
|
126
|
+
url : "Test",
|
127
|
+
dataType : "json",
|
128
|
+
success : function(data) {
|
129
|
+
|
130
|
+
$("#a")
|
131
|
+
|
132
|
+
for (j = 0; j < data.emp_list.length; j++) {
|
133
|
+
$("#abcd")
|
134
|
+
.append(
|
135
|
+
"<tr><td>"
|
136
|
+
+ data.emp_list[j].emp_id
|
137
|
+
+"</td><td>"
|
138
|
+
+ data.emp_list[j].emp_name
|
139
|
+
+ "</td></tr>")
|
140
|
+
}
|
141
|
+
|
142
|
+
},
|
143
|
+
```
|
144
|
+
```Java
|
145
|
+
package todo.controller;
|
146
|
+
|
147
|
+
import java.io.IOException;
|
148
|
+
import java.io.PrintWriter;
|
149
|
+
import java.sql.Connection;
|
150
|
+
import java.sql.PreparedStatement;
|
151
|
+
import java.sql.ResultSet;
|
152
|
+
import java.sql.SQLException;
|
153
|
+
import java.util.ArrayList;
|
154
|
+
import java.util.HashMap;
|
155
|
+
import java.util.Map;
|
156
|
+
|
157
|
+
import javax.servlet.RequestDispatcher;
|
158
|
+
import javax.servlet.ServletException;
|
159
|
+
import javax.servlet.annotation.WebServlet;
|
160
|
+
import javax.servlet.http.HttpServlet;
|
161
|
+
import javax.servlet.http.HttpServletRequest;
|
162
|
+
import javax.servlet.http.HttpServletResponse;
|
163
|
+
|
164
|
+
import org.apache.jasper.tagplugins.jstl.core.Out;
|
165
|
+
|
166
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
167
|
+
import com.fasterxml.jackson.core.JsonProcessingException;
|
168
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
169
|
+
import todo.controller.db;
|
170
|
+
|
171
|
+
/**
|
172
|
+
* Servlet implementation class HelloServlet
|
173
|
+
*/
|
174
|
+
@WebServlet("/Test")
|
175
|
+
public class Test extends HttpServlet {
|
176
|
+
|
177
|
+
static Connection connection = null;
|
178
|
+
static PreparedStatement preparedStatement = null;
|
179
|
+
static ResultSet resultSet = null;
|
180
|
+
ArrayList<Object> AllSearch_JSON = new ArrayList<Object>();
|
181
|
+
private static final long serialVersionUID = 1L;
|
182
|
+
String all_json = null;
|
183
|
+
/**
|
184
|
+
* @see HttpServlet#HttpServlet()
|
185
|
+
*/
|
186
|
+
public Test() {
|
187
|
+
super();
|
188
|
+
}
|
189
|
+
|
190
|
+
/**
|
191
|
+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
192
|
+
*/
|
193
|
+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
194
|
+
doPost(request, response);
|
195
|
+
}
|
196
|
+
|
197
|
+
/**
|
198
|
+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
199
|
+
*/
|
200
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
201
|
+
throws ServletException, IOException {
|
202
|
+
try {
|
203
|
+
ObjectMapper mapper = new ObjectMapper();
|
204
|
+
|
205
|
+
String json = mapper.writeValueAsString(emp.EmployeeList());
|
206
|
+
response.setContentType("application/json;charset=UTF-8");
|
207
|
+
PrintWriter pw = response.getWriter();
|
208
|
+
pw.print("{"+"\"emp_list\":"+json+"}");
|
209
|
+
} catch (Exception e) {
|
210
|
+
|
211
|
+
}
|
212
|
+
}
|
213
|
+
}
|
214
|
+
```
|
215
|
+
```json
|
216
|
+
//作成したJSONデータ
|
217
|
+
{
|
218
|
+
"emp_list": [{
|
219
|
+
"emp_id": 4,
|
220
|
+
"emp_name": "田中太郎"
|
221
|
+
}, {
|
222
|
+
"emp_id": 5,
|
223
|
+
"emp_name": "田中次郎"
|
224
|
+
}, {
|
225
|
+
"emp_id": 7,
|
226
|
+
"emp_name": "田中三郎"
|
227
|
+
}]
|
228
|
+
}
|
229
|
+
```
|
2
classが同じだったため変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -78,7 +78,7 @@
|
|
78
78
|
```
|
79
79
|
```Java
|
80
80
|
//Ajaxと連携するServlet
|
81
|
-
public class
|
81
|
+
public class HelloServlet2 extends HttpServlet {
|
82
82
|
|
83
83
|
private static final long serialVersionUID = 1L;
|
84
84
|
|
1
加筆しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
AjaxでServletに通信を行いsetAttributeの内容を
|
2
|
+
AjaxでServletに通信を行いsetAttributeの内容を描画したいのですがエラーが出て困っています。
|
3
|
+
エラー理由は、最初に読み込むjspの中にクリック時に読み込むsetAttributeが入っているためだと推測されます。どのように改修すれば良いのか教えていただけませんか?
|
3
4
|
|
4
5
|
### 発生している問題・エラーメッセージ
|
5
6
|
|
6
7
|
```
|
8
|
+
org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/view/index.jsp] at line [81]
|
7
|
-
|
9
|
+
Caused by: java.lang.NullPointerException
|
10
|
+
|
8
11
|
```
|
9
12
|
|
10
13
|
### 該当のソースコード
|
11
14
|
|
12
15
|
```java
|
13
|
-
package todo.controller;
|
14
|
-
|
15
16
|
/**
|
16
17
|
* 最初に呼び出される
|
17
18
|
*/
|
18
|
-
@WebServlet("/HelloServlet")
|
19
19
|
public class HelloServlet extends HttpServlet {
|
20
20
|
|
21
21
|
private static final long serialVersionUID = 1L;
|
@@ -56,53 +56,65 @@
|
|
56
56
|
}
|
57
57
|
```
|
58
58
|
```jsp
|
59
|
+
//DBのAの値を描画
|
60
|
+
<td id=abcd><%request.getAttribute("1")%><td>
|
61
|
+
|
59
62
|
$(function() {
|
60
|
-
$("#
|
63
|
+
$("#Search").click(function() {
|
61
|
-
.click(
|
62
|
-
function() {
|
63
|
-
|
64
|
+
// Ajax通信を開始する
|
64
|
-
$
|
65
|
-
|
65
|
+
$.ajax({
|
66
|
-
|
66
|
+
type : "POST",
|
67
|
-
|
67
|
+
url : "Test",
|
68
|
-
|
68
|
+
success : function() {
|
69
|
-
|
69
|
+
console.log("成功");
|
70
|
+
//DBのAの値を消してBの値に置き換える*NullPointerExceptionエラーが出る
|
71
|
+
$("#abcd").empty().append(<%request.getAttribute("2")%>)
|
70
|
-
|
72
|
+
},
|
71
|
-
|
73
|
+
error : function() {
|
72
|
-
|
74
|
+
console.log("失敗");}
|
73
|
-
$("#abcd")
|
74
|
-
|
75
|
+
})
|
75
|
-
.append(
|
76
|
-
<%int Employee_Size = (int) (request.getAttribute("All_Employee_Size"));
|
77
|
-
for (int i = 0; i < Employee_Size; i++) {
|
78
|
-
out.print("$(\"#abcd\").append(\"");
|
79
|
-
out.print("<tr>");
|
80
|
-
out.print("<td>" + request.getAttribute("All_emp_id" + i) + "</td>");
|
81
|
-
out.print("<td>" + request.getAttribute("All_emp_name" + i) + "</td>");
|
82
|
-
out.print("<td>" + request.getAttribute("All_gender" + i) + "</td>");
|
83
|
-
out.print("<td>" + request.getAttribute("All_birthday" + i) + "</td>");
|
84
|
-
out.print("<td>" + request.getAttribute("All_dept_name" + i) + "</td>");
|
85
|
-
out.print("<tr>");
|
86
|
-
out.print("\");");
|
87
|
-
|
76
|
+
});
|
77
|
+
});
|
78
|
+
```
|
79
|
+
```Java
|
88
|
-
|
80
|
+
//Ajaxと連携するServlet
|
89
|
-
.append(
|
90
|
-
|
81
|
+
public class HelloServlet extends HttpServlet {
|
91
82
|
|
83
|
+
private static final long serialVersionUID = 1L;
|
84
|
+
|
85
|
+
/**
|
86
|
+
* @see HttpServlet#HttpServlet()
|
92
|
-
|
87
|
+
*/
|
93
|
-
|
88
|
+
public HelloServlet() {
|
94
|
-
|
89
|
+
super();
|
95
|
-
|
90
|
+
}
|
91
|
+
|
92
|
+
/**
|
93
|
+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
94
|
+
*/
|
95
|
+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
96
|
+
doPost(request, response);
|
96
|
-
|
97
|
+
}
|
98
|
+
|
99
|
+
/**
|
100
|
+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
101
|
+
*/
|
102
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
103
|
+
throws ServletException, IOException {
|
104
|
+
try {
|
105
|
+
//DBに接続する BはDBの値
|
106
|
+
request.setAttribute("2","B")
|
107
|
+
|
108
|
+
} catch (Exception e) {
|
109
|
+
e.printStackTrace();
|
97
|
-
|
110
|
+
}
|
98
|
-
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
99
114
|
```
|
100
115
|
|
101
116
|
|
102
117
|
### 試したこと
|
118
|
+
Ajaxと通信するServletのsetAttribute内変数を最初に読み込むServletと同じにしてエラーを表示しないようにしましたが結局nullが出ており読み込めませんでした。
|
103
119
|
|
104
|
-
ここに問題に対して試したことを記載してください。
|
105
|
-
|
106
|
-
|
120
|
+
AjaxでServletと連携する場合setAttributeはしない方がよろしいのでしょうか?
|
107
|
-
|
108
|
-
ここにより詳細な情報を記載してください。
|