jquery取后台list

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");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值