问题描述:

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.

网友答案:

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)

网友答案:

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 + ')');
相关阅读:
Top