急,急,急 差点砸电脑了 !一个最简单的AJAX例子 怎么跑不起来
JSP部分:
<%@ page language= "java " import= "java.util.* " pageEncoding= "utf-8 "%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html>
<head>
<title> 无标题文档 </title>
<script language= "javascript ">
var XMLHttpReq = new ActiveXObject( "MSXML2.XMLHTTP.3.0 ");
function callServer() {
XMLHttpReq.open( "GET ", "servlet/ajax_sevelet ", "true ");
XMLHttpReq.onreadystatechange = updatePage;
XMLHttpReq.send(null);
}
function send_msg(){
callServer();
}
</script>
</head>
<body>
<form id= "form1 " name= "form1 " method= "post " action= " ">
姓名: <label>
<input type= "text " ondblclick= "send_msg() " name= "name " />
</label> <p>
密码: <label>
<input type= "text " name= "password " />
</label>
</form>
<a href= "servlet/ajax_sevelet "> ei </a> /* 点这里可以调用sevlet,证明路径没错啊
</body>
</html>
sevlet部分:
package shiyan;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ajax_sevelet extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy " string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int i=1;
response.setContentType( "text/html ");
PrintWriter out = response.getWriter();
String name=request.getParameter( "name ");
System.out.println(i+ "调用成功 "+name);
}
public void init() throws ServletException {
// Put your code here
}
}
web.xml部分:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app version= "2.4 "
xmlns= "http://java.sun.com/xml/ns/j2ee "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<servlet>
<description> This is the description of my J2EE component </description>
<display-name> This is the display name of my J2EE component </display-name>
<servlet-name> ajax_sevelet </servlet-name>
<servlet-class> shiyan.ajax_sevelet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ajax_sevelet </servlet-name>
<url-pattern> /servlet/ajax_sevelet </url-pattern>
</servlet-mapping>
</web-app>
可是跑不起来啊````我双击text (name) 提示网页上有错误....
哪为解决下 ,给我个简单的ajax例子也可以!
[解决办法]
XMLHttpReq.onreadystatechange = updatePage;//AJAX状态监听事件,当状态发生改变时调用updatePage()函数
function updatePage() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
parseResults();
}
}
}//当状态为4,说明AJAX完成调用,在判断xmlHttp.status == 200
就可以在parseResults()函数里处理结果了