帮我看看哪里错了。AJAX
JSP:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>2级联动</title>
<script type="text/javascript">
var xmlHttpRequest = new xmlHttpRequest;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
try{
xmlHttpRequest= new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttpRequest = new ActiveXObject("MsXM12.XMLHTTP");
}
}else if(window.xmlHttpRequest){
return new xmlHttpRequest;
}
}
function sendRequest(shengId){
var url ="${pageContext.request.contextPath}/ListServer?shengId=" +shengId;
if(shengId=0){
clearShi();
return;
}
xmlHttpRequest = createXMLHttpRequest();
xmlHttpRequest.onreadystatechange =showShi();
xmlHttpRequest.setRequestHeader("If-Modified-Since","0");
xmlHttpRequest.open("get",url,true);
xmlHttpRequest.send(null);
}
function showShi(){
alert("1");
var shi =document.getElementById("shi");
if(xmlHttpRequest.readyState == 4 &&xmlHttpRequest.readyState==200){
var str =xmlHttpRequest.responstText;
var shis=str.spit(",");
for(var i = 0;i<shis.length;i++){
var op = document.createElement("option");
op.text=shis[i];
op.valut=i;
shi.appendChild(op);
}
}
}
function clearShi(){
var shi =document.getElementById('shi');
shi.options.length=0;
shi.options[0] = new Option("请选择","0");
}
</script>
</head>
<body>
<h1>
2级连动
</h1>
省:
<select id='sheng' onchange="sendRequest(this.value)">
<option value="0">
请选择
</option>
<option value="1">
四川
</option>
<option value="2">
西藏
</option>
</select>
省:
<select id='shi'>
<option value="0">
请选择
</option>
</select>
</body>
</html>
Servlet:
import
java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListServer extends HttpServlet {
public static Map<String, List<String>> map = new HashMap<String, List<String>>();
@Override
public void init() throws
ServletException {
List<String> list = new ArrayList<String>();
list.add("成都");
list.add("自贡");
list.add("内江");
map.put("1", list);
list = new ArrayList<String>();
list.add("拉萨");
list.add("林芝");
map.put("2", list);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
String shengId = request.getParameter("shengId");
List<String> list = map.get(shengId);
StringBuffer sb = new StringBuffer();
if (list != null) {
for (String s : list) {
sb.append(s).append(",");
}
if (!list.isEmpty()) {
sb.deleteCharAt(sb.length() - 1);
}
}