jQuery post data to servlet

ChUcO

New Member
I'm having some problems posting data from a web page, using jQuery, to a servlet. While I'm an experienced Java developer, I'm very new to javascript/jQuery/servlets. I'm using Eclipse, Apache Tomcat, and Chrome.I have an XML file (from 6KB to 30MB in size) that I wish to load into the browser, modify, then post to the servlet.My HTML has:\[code\]<input id="filechooser" type="file" onchange="readFile()">\[/code\]My JS has:\[code\]var file = document.getElementById('filechooser').files[0];var reader;reader = new FileReader();reader.readAsText(file, "UTF-8");reader.onload = loaded;function loaded(evt){ var result = evt.target.result; var xml = $(result); ... [make modifications to xml]}\[/code\]Some jQuery code that I use in modifying the xml are \[code\]$(xml).find("NODE").val()\[/code\] and \[code\]$(xml).find("OTHER_NODE").attr("attribute-name","newValue")\[/code\]I now need to post that xml to a URL, where it will be used to process some information. In the Chrome console, I can view the content of the xml object:\[code\]> xml [<!--?xml version="1.0" encoding="ISO-8859-1"?-->,#text, <root_element>...</root_element>]> $(xml) [<!--?xml version="1.0" encoding="ISO-8859-1"?-->,#text, <root_element>...</root_element>]> console.dir(xml) jQuery.fn.jQuery.init[3] 0: #comment 1: #text 2: root_element length: 3 __proto__: Object[0]\[/code\]My servlet is empty so far:\[code\]protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Post");}\[/code\]I created a button that executes some javascript. The following two code snippets both post to the server:\[code\]$.post("http://localhost:8080/MyWebApp/MyWebAppUrl", xml);\[/code\]and:\[code\]$.ajax({ type: "POST", url: "http://localhost:8080/MyWebApp/MyWebAppUrl", data: xml});\[/code\]My problem is, I don't know if I'm sending my XML correctly, or how to properly consume it. What do I need to do to my jQuery code to post it correctly? How do I get it out of my HttpServletRequest? If I can get the xml text as a String, I know exactly how to manipulate it in Java, and get it to do whatever I want.After 10+ hours searching the web, I still can't find the answer. I'm sure it's out there, but I can't seem to connect the dots.
 
Back
Top