Ajax当中如何回传一个JSP
PassBackJsp
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
例 4.1
<%@ page contentType="text/html; charset=GBK"%>
<%@ page language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
table.default {
width:510px;
}
table.default td {
border:1px solid black;
height:23px;
}
table.default td.item {
background:#006589;
color:#fff;
text-align:center;
}
</style>
<script type="text/javascript">
var xmlHttp;
function createXmlHttp() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest(); //FireFox、Opera等浏览器支持的创建方式
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式
}
}
function refreshCart() {
createXmlHttp();
xmlHttp.onreadystatechange = showCartIn;
xmlHttp.open("GET", "cart.jsp" , true);
xmlHttp.send(null);
}
function showCartIn() {
if (xmlHttp.readyState == 4) {
/* thus, in this way, it return a jsp,so it is simple.if it return back a Servlet, it is difficult. this is a
very good idea. */
document.getElementById("shoppingcart").innerHTML = xmlHttp.responseText;
}
}
</script>
</head>
<body onload="refreshCart()">
<div id="shoppingcart">
</div>
</body>
</html>
cart.jsp
<%@page import="java.util.Date"%>
<%@ page contentType="text/html; charset=GBK"%>
<%@ page language="java"%>
<table class="default">
<tr>
<td width="35%" class="item">编号</td>
<td width="20%" class="item">名称</td>
<td width="15%" class="item">价格</td>
<td width="15%" class="item">功能</td>
</tr>
<tr>
<td>动态数据库获取</td>
<td align="center"><% out.print("马克-towin"); %></td>
<td align="center"><% Date t = new Date(); out.print(t); %></td>
<td align="center"><input type="button" onclick="refreshCart();" value="刷"></td>
</tr>
<tr >
<td>动态数据库获取</td>
<td>动态数据库获取</td>
<td align="center">动态数据库获取</td>
<td>动态数据库获取</td>
<tr>
<td colspan="4" style="height:50px" align="center">动态数据库获取</td>
</tr>
</table>