1

I need to create a webpage where the user submits an XML file, which I use to dynamically update the page.

I am following a tutorial and have this piece of code in a javascript file:

function uploadFile(){
    var path = "processXML?action=process&id=" + escape(fileChooser.value);
    req = initRequest();
    req.open("GET", path, true);
    req.onreadystatechange = callback;
    req.send(null);
}

I also have the URLpattern /processXMl set in a Java Servlet. Although I am learning something new, it's not what I am trying to achieve. The open() method is specifying a file on the server.

How do I read/receive an XML file from the client so I can process it within my Java classes on the server?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mead3000
  • 581
  • 2
  • 5
  • 18
  • I thought the ability to upload files with php might have been an option :s http://www.w3schools.com/php/php_file_upload.asp – Mead3000 Dec 19 '11 at 03:20
  • PHP doesn't run in webbrowser. It's HTML/CSS/JS which runs in webbrowser. – BalusC Dec 19 '11 at 04:24

1 Answers1

1

Sending alone the file name (or the file path in some poor browsers) of the selected file as a request parameter isn't going to work at all. You will not be able to retrieve the file contents in the server side in any way (unless both the webbrowser and webbrowser happen to run in physically the same machine, but this does of course not occur in real production). You need to let the client send the file contents instead. The file name is just metadata information.

Sending the file contents by ajax was not possible until HTML5/XHR2, which is relatively new and not is supported in all browsers which are currently still widely in use. I'd suggest to simulate the asynchronous file upload by a form in a hidden iframe instead. There are lot of JavaScript plugins out for this, such as jQuery Form and jQuery Uploadify.

To obtain the file contents in a servlet, you'd usually use Apache Commons FileUpload for this, or if you're already on Servlet 3.0, the new HttpServletRequest#getParts() method.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555