例子来源:《Ajax 基础教程》 金灵 等译 这本书非常不错
dynamicLists.html
<html>
<head>
<title>Dynamically Filling Lists</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function refreshModelList() {
var make = document.getElementById("make").value;
var modelYear = document.getElementById("modelYear").value;
if (make == "" || modelYear == "") {
clearModelsList();
return;
}
var url = "RefreshModelList?"
+ createQueryString(make, modelYear) + "&ts=" + new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function createQueryString(make, modelYear) {
var queryString = "make=" + make + "&modelYear=" + modelYear;
return queryString;
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
updateModelsList();
}
}
}
function updateModelsList() {
clearModelsList();
var models = document.getElementById("models");
var results = xmlHttp.responseXML.getElementsByTagName("model");
var option = null;
for (var i = 0; i < results.length; i++) {
option = document.createElement("option");
option.appendChild(document.createTextNode(results[i].firstChild.nodeValue));
models.appendChild(option);
}
}
function clearModelsList() {
var models = document.getElementById("models");
while (models.childNodes.length > 0) {
models.removeChild(models.childNodes[0]);
}
}
</script>
</head>
<body>
<h1>Select Model Year and Make</h1>
<form action="#">
<span style="font-weight:bold;">Model Year:</span>
<select id="modelYear" onchange="refreshModelList();">
<option value="">Select One</option>
<option value="2006">2006</option>
<option value="1995">1995</option>
<option value="1985">1985</option>
<option value="1970">1970</option>
</select>
<br/><br/>
<span style="font-weight:bold;">Make:</span>
<select id="make" onchange="refreshModelList();">
<option value="">Select One</option>
<option value="Chevrolet">Chevrolet</option>
<option value="Dodge">Dodge</option>
<option value="Pontiac">Pontiac</option>
</select>
<br/><br/>
<span style="font-weight:bold;">Models:</span>
<br/>
<select id="models" size="6" style="width:300px;">
</select>
</form>
</body>
</html>
RefreshModelListServlet.java
package ajaxbook.chap4;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RefreshModelListServlet extends HttpServlet {
private static List availableModels = new ArrayList();
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
int modelYear = Integer.parseInt(request.getParameter("modelYear"));
String make = request.getParameter("make");
StringBuffer results = new StringBuffer("<models>");
MakeModelYear availableModel = null;
for (Iterator it = availableModels.iterator(); it.hasNext();) {
availableModel = (MakeModelYear) it.next();
if (availableModel.modelYear == modelYear) {
if (availableModel.make.equals(make)) {
results.append("<model>");
results.append(availableModel.model);
results.append("</model>");
}
}
}
results.append("</models>");
response.setContentType("text/xml");
response.getWriter().write(results.toString());
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public void init() throws ServletException {
availableModels.add(new MakeModelYear(2006, "Dodge", "Charger1"));
availableModels.add(new MakeModelYear(1995, "Dodge", "Charger2"));
availableModels.add(new MakeModelYear(1985, "Dodge", "Charger3"));
availableModels.add(new MakeModelYear(1970, "Dodge", "Charger4"));
availableModels.add(new MakeModelYear(2006, "Dodge", "Charger5"));
availableModels.add(new MakeModelYear(2006, "Dodge", "Charger6"));
availableModels.add(new MakeModelYear(2006, "Dodge", "Charger7"));
availableModels.add(new MakeModelYear(2006, "Dodge", "Charger8"));
}
private static class MakeModelYear {
private int modelYear;
private String make;
private String model;
public MakeModelYear(int modelYear, String make, String model) {
this.modelYear = modelYear;
this.make = make;
this.model = model;
}
}
}