将arraylist的值传递给javascript

时间:2013-10-04 18:28:29

标签: javascript jsp arraylist

我有一个jsp.i中的arraylist在另一个jsp的java脚本中想要arraylist值。你能告诉我怎么做。以下是我的code.i有两个jsp。

getuser.jsp


Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
            String query = "select user_registeration_code from system_user_master where user_first_name ='"+firstname+"' and user_last_name='"+lastname+"' and user_phone_no='"+emp_id+"'";
       st = conn.createStatement();

       ArrayList<String> addressLists1 = new ArrayList<String>();
           ResultSet  rs = st.executeQuery(query);
           while(rs.next())
                {


               addressLists1.add(rs.getString("user_registeration_code"));


                }
                out.println(addressLists1.size());
                //out.println(data);
                request.setAttribute("top", addressLists1);

second.jsp


< script type = "text/javascript" >


function showEmp(emp_value) {

    var fname = document.getElementById("txtfirstname").value;
    alert(fname);
    var lname = document.getElementById("txtlastname").value;
    alert(lname);
    if (document.getElementById("txtmobileno").value != "") {
        alert("1");
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert("2");
            alert("Browser does not support HTTP Request");
            return;
        }
        var url = "getuser.jsp";
        url = url + "?emp_id=" + emp_value + "&firstname=" + fname + "&lastname=" + lname;

        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);

    } else {
        alert("Please Select Employee Id");
    }
}

function stateChanged() {
    alert("3");
    document.getElementById("txtfirstname").value = "";
    document.getElementById("txtmobileno").value = "";
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

        alert("4");
        var showdata = [];
        showdata = xmlHttp.responseText;
        alert("qqqqqqqqq" + showdata.length);
        var i;
        for (i = 0; i < showdata.length; i++) {
            alert("----------------" + showdata.get(i));
        }
    }
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

< /script>

1 个答案:

答案 0 :(得分:0)

首先,在getuser.jsp中更改

 request.setAttribute("top", addressLists1);

 session.setAttribute("top", addressLists1);

然后在second.jsp中添加类似

的内容
 ArrayList<String> addressLists1 = (ArrayList<String>)session.getAttribute("top");

然后遍历它并将列表打印到你的html / javascript输出中。

顺便说一句,如果您要连接到JSP中的数据库,那么至少连接到数据库的两行代码应该移动到您在JSP而不仅仅是在JSP本身中拥有这些行。在JSP中拥有数据库连接部分是一个非常糟糕的主意。如果您的数据库服务器名和密码在JSP中,并且JSP遇到运行时错误,导致它向用户显示有问题的代码行,那么您的数据库密码可能会暴露给用户。