简体   繁体   中英

How to send array from servlet and receive in HTML jquery?

I am parsing an uploaded XML file using dom, generating the string where hostname and osname is made into a string separated with a , delimiter. s is the variable with this string and I am sending it back to HTML using response.getWriter object obj . But instead of printing it eg: Windows,Abhishek I'd like to split it with the delimiter , . Can someone show me example code of how I can receive this string in jQuery or JS and then split it into two strings?

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 part:

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

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

The alert(data); prints "Using Commons File Upload" but in the servlet out.println(s); does not give me my data in the alert, instead it comes blank.

You state that you're sending a comma-delimited string to the browser, and you want to split it by comma in Javascript/JQuery. Is that a reasonable summary of your question? I found it quite hard to read, but I think that's what you're asking, so that's what I'll answer. :)

Javascript has a .split() method, which you can use on any string variable.

So where you receive the string in your Javascript code, you can simply do something like this:

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

Hope that helps. (though I do get the feeling there's more to the question than my interpretation)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM