jquery取靠山list

具体请点击:http://www.verydemo.com/demo_c110_i6437.html

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");
       
        var text = "";
        if(items[i].childNodes.length > 0)
            text = items[i].firstChild.nodeValue;       
        var nText = document.createTextNode(text);
        option.appendChild(nText);
        list.appendChild(option);
    }
}

</script>
</head>
<body onload="initList();">
<select id="list" size="5" ></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返回的内容不允许客户端缓存
        resp.setHeader("cache-control", "no-cache");        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");
    }
}

相关推荐