html code
<!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">
<script type="text/javascript">
function initList(){
var xhr;
if(window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else if(window.ActiveXObject)
xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("get", "showListServlet", true);
xhr.onreadystatechange = function(){
if(xhr.status == 200 && xhr.readyState == 4){
parseXML2List(xhr.responseXML);
}
}
xhr.send(null);
}
function parseXML2List(xml){
var list = document.getElementById('list');
while(list.childNodes.length>0)
list.removeChild(list.firstChild);
var items = xml.getElementsByTagName("item");
for(var i=0;i<items.length;i++){
var option = document.createElement("option");
[color=#FF0000]var text = "";
if(items[i].childNodes.length > 0)
text = items[i].firstChild.nodeValue;[/color]
var nText = document.createTextNode(text);
option.appendChild(nText);
list.appendChild(option);
}
}
</script>
</head>
<body οnlοad="initList();">
<select id="list" size="5" style="width: 100px">
</select>
</body>
</html>
servlet code
package com.liyh.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("unchecked")
public class ShowListServlet extends HttpServlet {
private static final long serialVersionUID = -6920365688165262694L;
private final List list = new ArrayList();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//servlet返回的内容不允许客户端缓存
[color=#FF0000]resp.setHeader("cache-control", "no-cache");[/color] resp.setContentType("text/xml;charset=UTF-8");
PrintWriter out = resp.getWriter();
StringBuilder xml = new StringBuilder();
xml.append("<items>");
for (Object o : list) {
xml.append("<item>").append(o).append("</item>");
}
xml.append("</items>");
out.print(xml.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void init() throws ServletException {
list.add("xxaaaa");
list.add("");
list.add("xxbbbb");
list.add("");
list.add("xxcccc");
list.add("xxdddd");
}
}
<!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">
<script type="text/javascript">
function initList(){
var xhr;
if(window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else if(window.ActiveXObject)
xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("get", "showListServlet", true);
xhr.onreadystatechange = function(){
if(xhr.status == 200 && xhr.readyState == 4){
parseXML2List(xhr.responseXML);
}
}
xhr.send(null);
}
function parseXML2List(xml){
var list = document.getElementById('list');
while(list.childNodes.length>0)
list.removeChild(list.firstChild);
var items = xml.getElementsByTagName("item");
for(var i=0;i<items.length;i++){
var option = document.createElement("option");
[color=#FF0000]var text = "";
if(items[i].childNodes.length > 0)
text = items[i].firstChild.nodeValue;[/color]
var nText = document.createTextNode(text);
option.appendChild(nText);
list.appendChild(option);
}
}
</script>
</head>
<body οnlοad="initList();">
<select id="list" size="5" style="width: 100px">
</select>
</body>
</html>
servlet code
package com.liyh.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("unchecked")
public class ShowListServlet extends HttpServlet {
private static final long serialVersionUID = -6920365688165262694L;
private final List list = new ArrayList();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//servlet返回的内容不允许客户端缓存
[color=#FF0000]resp.setHeader("cache-control", "no-cache");[/color] resp.setContentType("text/xml;charset=UTF-8");
PrintWriter out = resp.getWriter();
StringBuilder xml = new StringBuilder();
xml.append("<items>");
for (Object o : list) {
xml.append("<item>").append(o).append("</item>");
}
xml.append("</items>");
out.print(xml.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void init() throws ServletException {
list.add("xxaaaa");
list.add("");
list.add("xxbbbb");
list.add("");
list.add("xxcccc");
list.add("xxdddd");
}
}