//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>Insert title here</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttp(){
xmlHttp=new XMLHttpRequest();
}
function showMsg(){
createXMLHttp();
xmlHttp.open("POST","NewFile.jsp");
xmlHttp.onreadystatechange=showMsgCallback;
xmlHttp.send(null);
}
function showMsgCallback(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var text=xmlHttp.responseText;
document.getElementById("msg").className="样式表名称";
document.getElementById("msg").innerHTML=text;
}
}
}
</script>
</head>
<body>
<input type="button" οnclick="showMsg()" value="调用Ajax" >
<span id="msg"></span>
</body>
</html>
调用的jsp文件名:NewFile.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello Ajax!
</body>
</html>