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.
sis simply never populated.