0
我想要獲取所有錶行並將它們添加到hashmap數組中,然後將它們轉換爲json對象以使用ajax和jquery發送到jsp頁面。我在顯示jsp頁面中的內容時遇到問題。 我已經設法編寫下面的代碼,需要幫助來糾正/改進它,我很困惑如何在jsp頁面中顯示它。我是jQuery和json的初學者。將HahMap數組轉換爲json對象並將其顯示在jsp頁面
try {
String res = request.getParameter("hello");
String user = "";
String pass = "";
String port = "";
String dbname = "";
String host = "";
String driver = "oracle.dbc.driver.OracleDriver";
Connection con;
PreparedStatement ps;
ResultSet rs;
String json = null;
String url = "jdbc:oracle:thin:@"+host+":"+port+":"+dbname;
Map<String,String> map = new HashMap<String,String>();
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,user,pass);
ps = con.prepareStatement("select * from employee");
rs = ps.executeQuery();
while(rs.next()){
map.put("name", rs.getString(1));
map.put("id", rs.getString(2));
map.put("salary",rs.getString(3));
list.add(map);
}
json = new Gson().toJson(list);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}catch(Exception e){
}
} finally {
out.close();
}
的index.jsp
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#push").click(function(){
var value = $("#t").val();
$.post('jsonServlet',{hello:value},function(data){
// ??
// ??
// ??
});
});
});
</script>
</head>
<body style="position: absolute;min-height: 700px;min-width: 1300px">
<form>
<input type="button" value="PushMe" id="push">
<input type="text" id="t" value="my data">
<div style="position: absolute;height: 50%;width: 60%;" id="myDiv">
</div>
</form>
</body>
需要幫助的JSP頁面來顯示在DIV哈希表的內容...... 後來我將改變DIV使用文本框,存儲各個列值,以便該用戶可以修改表格的內容並再次更新表格行。
需要幫助
關於()的設計:從現在開始,JavaScript向下循環比前向循環要快上百倍,在所有瀏覽器中一致。緩存數組的長度幾乎沒有任何影響。 http://jsperf.com/loopy-arrays/3 – enlait
謝謝你,先生,真的很有幫助。 – Kharoud
你好先生我知道我標記爲已解決,但仍然存在問題。我沒有從我的jsp中獲取servlet的值。甚至沒有簡單的字符串。在我的gson()方法中有沒有問題,或者在Ajax調用中有沒有問題? – Kharoud