tags:

views:

283

answers:

1

Hi all. In Javascript I can send XML string to JSP server (XmlAction.jsp):

Javascript Code:

var xmlDocument = "<?xml version="1.0" encoding='ISO-8859-1'?>
                        <Customer>
                             .......
                         </Customer> 
                    "
 var httpRequest;

 httpRequest = new XMLHttpRequest();    
 httpRequest.open('POST', "http://example.com/XmlAction.jsp", false);
 httpRequest.send(xmlDocument);

My question: How can I get XML data in JSP Page XmlAction.jsp ?

Can you give me some java code. Thank all ^^

A: 

Call request.getInputStream() to get a stream of the submitted XML body, and pass it to whatever XML parser you plan to use (eg. for DOM an implementation of DocumentBuilder.parse(stream), which you might get from javax.xml.parsers.DocumentBuilderFactory).

Having a non-web-form (application/x-www-form-urlencoded, multipart/form-data) POST body is slightly unusual for Servlet/JSP but should work okay. Just make sure nothing is calling methods like getParameter() which expect web form input, and will get confused if you've already read the input stream.

[aside: do you really mean ISO-8859-1? If you send Unicode characters in a body with XMLHttpRequest.send(), they will be encoded as UTF-8, which won't match your XML declaration.]

bobince