我有以下代码:
package server;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.simple.JSONValue;
@WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public servlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
JSONObject responseData=new JSONObject();
JSONArray jArray=new JSONArray();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8080/neue","*****","****");
PreparedStatement prepared = con.prepareStatement("Select * from person");
ResultSet rs = prepared.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while(rs.next())
{
JSONObject jObject=new JSONObject();
JSONArray Jarr = new JSONArray();
Jarr.add(rs.getString("a"));
Jarr.add(rs.getString("b"));
Jarr.add(rs.getString("c"));
Jarr.add(rs.getString("d"));
Jarr.add(rs.getString("e"));
jObject.put("cell", Jarr);
jArray.add(jObject);
}
responseData.put("total", 5);
responseData.put("page", 1);
responseData.put("records",numberOfColumns);
responseData.put("rows",jArray);
out.println(JSONValue.toJSONString(responseData));
} catch (Exception e) {
}
}
}
Script.js看起来像这样:
$(document).ready(function (){
$("#list").jqGrid({
url:'servlet',
datatype: "json",
mtype : 'get',
colNames:['a','b', 'c', 'd','e'],
colModel:[
{name:'a',index:'a', width:75},
{name:'b',index:'b', width:90},
{name:'c',index:'c', width:100},
{name:'d',index:'d', width:80,align:"right"},
{name:'e',index:'e', width:80, align:"right"},
],
rowNum:10,
autowidth: true,
rowList:[10,20,30],
pager: jQuery('#pager'),
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"JSON Example",
jsonReader: {
repeatitems: false,
id: "0",
cell: "",
root: "rows",
page: function() { return 1; },
total: function() { return 1; },
records: function(obj) { return obj.length; }
}
}).navGrid('#pager',{edit:false,add:false,del:false});
});
正如您所见,我尝试从servlet获取数据。 但我没有得到他们的回应。
表格仍然是空的,只是我能看到的标题。 在firebug控制台中,我收到了这条消息:
状态代码:500内部服务器错误
我该怎么做才能运行该程序?
非常感谢