<html>
<head>
<script type="text/javascript">
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest(); //To support the browsers IE7+, Firefox, Chrome, Opera, Safari
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); // For the browsers IE6, IE5
} else {
alert("Error due to old verion of browser upgrade your browser");
}
}
var rcvReq = getXmlHttpRequestObject();
function alterContent() {
if (rcvReq.readyState == 4 || rcvReq.readyState == 0) {
rcvReq.open("GET", 'AccessServlet', true);
rcvReq.onreadystatechange = handleAlterContent;
rcvReq.send(null);
}
}
function handleAlterContent() {
if (rcvReq.readyState == 4) {
document.getElementById("1").innerHTML = rcvReq.responseText;
}
}
</script>
</head>
<body>
<h2>Ajax Demo</h2>
<p id="1">
<input type="button" οnclick="alterContent()" value="AlterContent" />
</p>
</body>
</html>
<servlet>
<servlet-name>AccessServlet</servlet-name>
<servlet-class>ajax.AccessServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AccessServlet</servlet-name>
<url-pattern>/AccessServlet</url-pattern>
</servlet-mapping>
原文:http://www.devmanuals.com/tutorials/java/servlet/ajax/accessServlet.html
源代码:http://pan.baidu.com/share/link?shareid=481430&uk=3878681452