如何从servlet发送数组并在HTML jquery中接收?

时间:2011-04-21 06:43:13

标签: java javascript html servlets jquery

我正在使用dom解析上传的XML文件,生成字符串,其中hostname和osname被生成一个用,分隔符分隔的字符串。 s是包含此字符串的变量,我使用response.getWriter对象obj将其发送回HTML。但不是打印它,例如:Windows,Abhishek我想用分隔符,拆分它。有人能告诉我如何在jQuery或JS中接收此字符串然后将其拆分为两个字符串的示例代码吗?

try {
    out.println("Using Commons File Upload");
    List items = uploadHandler.parseRequest(request);
    Iterator itr = items.iterator();
    String str=null;
    while(itr.hasNext()) {
        FileItem item = (FileItem) itr.next();

        if(item.isFormField()) {

            /*out.println("Form Input Name = "+item.getFieldName()+", Form Input Value = "+item.getString());*/
        } else {
            /*out.println("Field Name = "+item.getFieldName()+
                ", File Name = "+item.getName()+
                ", Content type = "+item.getContentType()+
                ", File Size = "+item.getSize());   */              
            File file = new File(destinationDir,item.getName());

            item.write(file);

            str=item.getName();

            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                File xmlFile = new File(destinationDir,str);

                if (file.exists()) {
                    Document doc = db.parse(xmlFile);
                    Element docEle = doc.getDocumentElement();

                    NodeList csmList = docEle.getElementsByTagName("system");

                    if (csmList != null && csmList.getLength() > 0) {
                        for (int i = 0; i < csmList.getLength(); i++) {

                            Node node = csmList.item(i);

                            if (node.getNodeType() == Node.ELEMENT_NODE) {

                                Element e = (Element) node;
                                NodeList nodeList = e.getElementsByTagName("hostname");
                                s=nodeList.item(0).getChildNodes().item(0).getNodeValue();
                                //System.out.println("HOSTNAME: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());

                                nodeList = e.getElementsByTagName("osname");
                                s+=","+nodeList.item(0).getChildNodes().item(0).getNodeValue();
                                //System.out.println("OSNAME: " + nodeList.item(0).getChildNodes().item(0) .getNodeValue());
                            }
                        }
                        out.println(s);
                    }
                }
                else {
                    System.out.println("File Not Found");
                }

            }
            catch (Exception e) {
                System.out.println(e);
            }

        }

    }
    out.close();
    System.out.println(str);


}catch(FileUploadException ex) {
    log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
    log("Error encountered while uploading file",ex);
}

Javascript部分:

function postData() {
    $.post("/com/FileUploadServlet", { "file": "/com/FileUploadServlet" }, function(data) {
        alert(data); 
    });
}

$(document).ready(function() {
    postData();
});

alert(data);打印"Using Commons File Upload"但是在servlet out.println(s);中没有在警报中提供我的数据,而是空白。

2 个答案:

答案 0 :(得分:1)

您声明您正在向浏览器发送逗号分隔的字符串,并且您希望在Javascript / JQuery中用逗号分隔它。这是对你的问题的合理总结吗?我觉得很难读,但我认为这就是你要问的问题,这就是我要回答的问题。 :)

Javascript有一个.split()方法,您可以在任何字符串变量上使用它。

因此,如果您在Javascript代码中收到字符串,则可以执行以下操作:

var splitstring = inputstring.split(',');

希望有所帮助。 (虽然我确实感觉到这个问题比我的解释更多)

答案 1 :(得分:0)

例如,您可以将java中的数组转换为JSON字符串,然后在javascript端从中创建一个对象,如下所示:

var myObject = eval('(' + myJSONtext + ')');