实体
package dhy.entity;
public class Book {
public Book(Integer id, String name, String author, Integer price){
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
public Integer id;
public String name;
public String author;
public Integer price;
public Integer getid() {
return id;
}
public void setid(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getprice() {
return price;
}
public void setprice(Integer price) {
this.price = price;
}
}
模拟数据库
package dhy.service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import dhy.entity.Book;
public class BookService {
//模拟内存中的数据库
static Map<Integer, List<Book>> bookDB = new LinkedHashMap<Integer, List<Book>>();
static{
//初始化 bookDB 对象
List<Book> list1 = new ArrayList<Book>();
List<Book> list2 = new ArrayList<Book>();
List<Book> list3 = new ArrayList<Book>();
list1.add(new Book(1, "书籍1", "作者1", 109));
list1.add(new Book(2, "书籍2", "作者1", 99));
list1.add(new Book(3, "书籍3", "作者1", 89));
list2.add(new Book(4, "书籍4", "作者2", 23));
list2.add(new Book(5, "书籍5", "作者3", 20));
list3.add(new Book(6, "书籍6", "作者4", 16));
list3.add(new Book(7, "书籍7", "作者5", 18));
bookDB.put(1, list1);
bookDB.put(2, list2);
bookDB.put(3, list3);
}
public List<Book> getBookByCategory(int categoryId){
return bookDB.get(categoryId);
}
public static void main(String[] args){
List<Book> list1 = new ArrayList<Book>();
list1.add(new Book(1, "书籍1", "作者1", 109));
list1.add(new Book(2, "书籍2", "作者1", 99));
list1.add(new Book(3, "书籍3", "作者1", 89));
JSONArray array = new JSONArray();
array = JSONArray.fromObject(list1);
System.out.println(array);
}
}
servlet
package dhy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import dhy.entity.Book;
import dhy.service.BookService;
public class ChooseBookServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
String idStr = (String)request.getParameter("id");
int id = (idStr == null) ? 1:Integer.parseInt(idStr);
List<Book> books = new BookService().getBookByCategory(id);
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
JSONArray array = new JSONArray();
array = JSONArray.fromObject(books);
out.println(array);
}
public static void main(String[] args){
List<Book> books = new BookService().getBookByCategory(2);
System.out.println(JSONArray.fromObject(books));
}
}
其他都没有问题,但是在页面中通过ajax与servlet通信时,报500错误
var xmlrequest;
//定义处理响应的回调函数
function processResponse(){
//响应完成且响应正常
if(xmlrequest.readyState == 4){
if(xmlrequest.status == 200){
var bookTb = document.getElementById("book");
//删除 bookTb 原有的所有行
while(bookTb.rows.length > 0){
bookTb.deleteRow(bookTb.rows.length - 1);
}
//获取服务器的 JSON 响应
//并调用 eval() 函数将服务器响应解析成 JavaScript 数组
var books = eval(xmlrequest.responseText);
//遍历数组,每个数组元素生成一个表格
for(var i=0,len=books.length; i<len; i++){
var tr= bookTb.insertRow(i);
//依次创建4个单元格,并未单元格设置内容
var cell0 = tr.insertCell(0);
cell0.innerHTML = books[i].id;
var cell1 = tr.insertCell(1);
cell1.innerHTML = books[i].name;
var cell2 = tr.insertCell(2);
cell2.innerHTML = books[i].author;
var cell3 = tr.insertCell(3);
cell3.innerHTML = books[i].price;
}
}else{
//页面不正常
window.alert("您所请求的页面有异常");
}
}
}
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
xmlrequest = new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getBookInfo(){
var id=2;
createXMLHttpRequest();
//创建 url 链接
var uri = "ChooseBookServlet";
//打开链接
xmlrequest.open("POST", uri, true);
//设置响应头
xmlrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//指定当 onreadystatechange 改变时的响应函数
xmlrequest.onreadystatechange = processResponse;
//发送请求
xmlrequest.send("id="+id);
}
------解决思路----------------------
没异常信息,不好判断
------解决思路----------------------
页面与servlet的数据传输始终是String类型的,我个人觉得你不应该这样转:
JSONArray array = new JSONArray();
array = JSONArray.fromObject(books);
而是应该把books转成json 的String格式 。
比如 String json = JSON.toJSONStringWithDateFormat(books, "yyyy-MM-dd HH:mm:ss"); 然后把json传到页面。
你可以试一试,我没有验证,仅仅看了一下代码后给出的观点。