你可以使用AJAX(使用jQuery很容易),拨打电话到你的servlet现在
function callMe(){
$.ajax({
type: "POST",
url: "/someServlet",
data: { param1: "val1" , param2: "val2" }
}).done(function(data) {
//TODO
});
}
在Servlet中,在doPost()
,使用GSON产生JSON表示您的收藏
String parameter1 = request.getParameter(param1);
String parameter2 = request.getParameter(param2);
//call to service to generate the collection
//for example List<Employee>
List<Employee> employees = someService(parameter1, parameter2);
//using google's gson
Gson gson = new Gson();
String json = new Gson().toJson(employees);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
现在我们在javascript函数中作为javascript对象数组的响应,所以修改为
}).done(function(data) {
//some processing for display
var len = data.length
for (var i=0; i<len; ++i) {
var employeeFirstName = data[i].firstName;
var employeeLastName = data[i].lastName;
//set it to some DIV, or do the processing you want
}
}
});
另请参见
我不确定这是否适用:将数据序列化为JSON并使用AJAX加载数据;然后使用Javascript来显示收到的JSON中的数据。 – nhahtdh
或者,您可以使用嵌入当前JSP页面的iframe(显示另一个处理您的java List数据的JSP页面)。 AJAX是另一种选择。 –