I am trying to write a single Array object using PrintWriter.out.write()
as a response to a JavaScript (AJAX) function,
Is this possible? or will I need to convert the array in same manner before sending?
Trying to avoid creation of comma separated String and then parsing in JS
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write(listDir());
}
private File[] listDir(){
File folder = new File("/home/rob/");
File[] listOfFiles = folder.listFiles();
//System.out.println("Number of files found: " + listOfFiles.length);
return listOfFiles;
}
Would like to then iterate through the files within the calling JavaScript AJAX function:
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
for (var i = 0; i < xmlhttp.responseText.length; i++) {
alert(xmlhttp.responseText[i]);
}
}