Home > Back-end >  JSP display MySQL data in json
JSP display MySQL data in json

Time:09-27

I use a json in JSP page display MySQL data but no response, bosses can help have a look, I also connected to the database, run light Java program can get to the data, but can't get to the page by

TestJson.
JSP program<% @ page language="Java" contentType="text/HTML. Charset=utf-8 "
PageEncoding="utf-8" % & gt;


<meta HTTP - equiv="content-type" Content="text/HTML. Charset=utf-8 "& gt;
Test
<style type="text/CSS" & gt;
</style>
//the json data is read from the request
Var jsonStr=${requestScope. Json}
//the console. The log (' jsonStr='+ jsonStr);
//the console. The log (jsonStr);

The function showDataToDiv (tagId) {
Var div1Node=document. GetElementById (tagId);
for (var i=0; i Var inputNode=document. The createElement method (" input ");
Var brNode=document. The createElement method (" br ");
Var br2Node=document. The createElement method (" br ");
InputNode. Type="text";
InputNode. Size=500;
InputNode. [I] value=https://bbs.csdn.net/topics/jsonStr;
Div1Node. The appendChild (inputNode);
Div1Node. The appendChild (brNode);
Div1Node. The appendChild (br2Node);
}
}
Window. The onl oad=function () {
Alert (jsonStr. Length);
ShowDataToDiv (" div1 ");
}

</script>

<body>





Java program

@ WebServlet ("/TestJson ")
Public class TestJson extends the HttpServlet {
private static final long serialVersionUID=1L;

Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
DBData DBData=https://bbs.csdn.net/topics/new DBData ();
List Data=(https://bbs.csdn.net/topics/dbData.getDBData);
JSONArray JSONArray=JSONArray. FromObject (data);
System. The out. Println (jsonArray. IsEmpty ());
String json=jsonArray. ToString ();
System. The out. Println (json);
Request. SetAttribute (" json ", json);

Request. GetRequestDispatcher (" testJson. JSP "). The forward (request, response);
}

Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response);
}

}
  • Related