test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="./test.js"></script> </head>
<body>
<form action="" enctype="application/x-www-form-urlencoded">
<input type="button" id="ok" name="ok" value="连接服务器">
</form>
</body>
</html>test.js
/**
* 步骤:
* 1、创建XMLHttpRequest对象
* 2、打开和服务器的连接
* 3、发送数据
* 4、接收服务器端的响应
* @returns {Boolean}
*//**
* 1、创建XMLHttpRequest对象
*/
function ajaxFunction() {
var xmlHttp; try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return xmlHttp;
}window.onload = function() {
document.getElementById("ok").onclick = function() {
alert("AAA");
/*
* 1、获取XMLHttpRequest对象
*/
var xmlReq = ajaxFunction();
alert("BBB");
/*
* 2、打开和服务器的连接
*/
xmlReq.open("get","../TestServlet",true);
alert("CCC");
/*
* 3、发送数据
*/
/*
* 4、响应服务器的响应
*/
}
}TestServlet.java
package com.chk.ajax;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 TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
}}web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.chk.ajax.TestServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>服务器起来之后运行test.jspalert("AAA");
alert("BBB");
可以正常弹出;
alert("CCC");
弹不出来
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="./test.js"></script> </head>
<body>
<form action="" enctype="application/x-www-form-urlencoded">
<input type="button" id="ok" name="ok" value="连接服务器">
</form>
</body>
</html>test.js
/**
* 步骤:
* 1、创建XMLHttpRequest对象
* 2、打开和服务器的连接
* 3、发送数据
* 4、接收服务器端的响应
* @returns {Boolean}
*//**
* 1、创建XMLHttpRequest对象
*/
function ajaxFunction() {
var xmlHttp; try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return xmlHttp;
}window.onload = function() {
document.getElementById("ok").onclick = function() {
alert("AAA");
/*
* 1、获取XMLHttpRequest对象
*/
var xmlReq = ajaxFunction();
alert("BBB");
/*
* 2、打开和服务器的连接
*/
xmlReq.open("get","../TestServlet",true);
alert("CCC");
/*
* 3、发送数据
*/
/*
* 4、响应服务器的响应
*/
}
}TestServlet.java
package com.chk.ajax;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 TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
}}web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.chk.ajax.TestServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>服务器起来之后运行test.jspalert("AAA");
alert("BBB");
可以正常弹出;
alert("CCC");
弹不出来
<url-pattern>/TestServlet</url-pattern>
你的ajax函数里写的却是
xmlReq.open("get","../TestServlet",true);把路径改下试试
xmlReq.open("get","/TestServlet",true);
我的servlet在src目录下,并且建了com.chk.ajax的包所以必须"../TestServlet"这样写
改为:xmlReq.open("get","/TestServlet",true);
还是不行啊