我用json在jsp頁面顯示MySQL資料但是沒有反應,大佬們可以幫忙看看嗎,資料庫我也連上了,光Java程式可以運行獲取到資料,但到頁面就獲取不到了
testJson.jsp程式
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
<style type="text/css">
</style>
<script type="text/javascript">
//從request中取出json資料
var jsonStr = ${requestScope.json}
//console.log('jsonStr=' + jsonStr);
//console.log(jsonStr);
function showDataToDiv(tagId){
var div1Node = document.getElementById(tagId);
for (var i = 0; i < jsonStr.length; i++) {
var inputNode = document.createElement("input");
var brNode = document.createElement("br");
var br2Node = document.createElement("br");
inputNode.type = "text";
inputNode.size = 500;
inputNode.value = jsonStr[i];
div1Node.appendChild(inputNode);
div1Node.appendChild(brNode);
div1Node.appendChild(br2Node);
}
}
window.onload = function() {
alert(jsonStr.length);
showDataToDiv("div1");
}
</script>
</head>
<body>
<h2>sql</h2>
<div id="div1"></div>
</body>
</html>
Java程式
@WebServlet("/TestJson")
public class TestJson extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
DBData dbData = new DBData();
List<String> data = dbData.getDBData();
JSONArray jsonArray = JSONArray.fromObject(data);
System.out.println(jsonArray.isEmpty());
String json = jsonArray.toString();
System.out.println(json);
request.setAttribute("json", json);
request.getRequestDispatcher("testJson.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/82200.html
標籤:Java相關
上一篇:SpringCloud Config Client讀取不到server端的值總是報錯
下一篇:docker+jenkins+springboot parent.relativePath points at no local POM Non-resolva
