0

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 e.g.: 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.

1
  • Debug the code. What code in servlet get executed and what not? The symptoms indicate that the s is simply never populated. Commented Apr 21, 2011 at 11:24

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the info. Sorry for such big question, in short I'd like to know, w.r.t my code how will I receive the comma-delimited string in the same HTML page from where I submitted the upload file.
I managed to work it out, not by sending array but a string of text separated by , delimiter. Here is the link if some1 wants to have a look. elmicoxcodes.blogspot.com/2007/03/…
0

For example, you can convert an array in java as JSON string, and then on the javascript side make an object from it like this:

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

3 Comments

Hi thanks for your reply. I used JSON, but now from inside DefaultHandler how do i pass the JSON Object? JSONObject obj=new JSONObject(); obj.put("host",summ[0]); obj.put("name",summ[1]);
Can you show me an example that will help me to pass this JSON Object from inside of DefaultHandler block and use it in jquery?
Hey, I edited my code. Now it seem much easier to pass data, as its only array of strings, so I can pass with some delimiter. I'd like to know how would I receive the strings from my servlet to the same html page from where I uploaded file, to show the string (servlet returned string).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.