0

I'm trying to send a XML to a jsp using XMLHttpRequest. Can anybody tell me how to read the request XML in jsp? Below is the code i'm using to do a POST call.

Thanks in advance!

var xmlHttpRequest=new XMLHttpRequest();
xmlHttpRequest.open("POST",url,true);
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml");
xmlHttpRequest.send("<xml><user_id>F1528</user_id></xml>");

2 Answers 2

1

JSP's should be used to generate pages and Servlets should be used to process the business logic.

I would suggest you to read the request body in Servlet. Set the read body to the request attribute and forward the request to JSP which renders the request body.

3
  • ramesh - you are correct. just for now, i want to know how its done in jsp. if i do a http GET, i read the parameters in the jsp using request.getParameters. how can i parse an XML sent using http POST?
    – arvind
    Jun 25, 2012 at 7:05
  • In that case, you can use the tag provided by JSTL.<%@ taglib prefix="x" uri="java.sun.com/jsp/jstl/xml" %> <x:parse doc="${pageContext.request.reader}" var="parsedXml"/>
    – Ramesh PVK
    Jun 25, 2012 at 7:09
  • I have to write the POST data to a file. Thats why for the time being im trying to do this in jsp. can this be done in a scriptlet?
    – arvind
    Jun 25, 2012 at 9:19
0

I haven't tried this, but it seems like JSTL should provide an easy solution:

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<x:parse doc="${pageContext.request.reader}" var="parsedXml"/>

Which should result in a page-scoped var called parsedXml.

2
  • kschneid - is there a way to do this in a scriptlet?
    – arvind
    Jun 25, 2012 at 9:21
  • Of course you can do it in a scriptlet, it's just more work. Calling request.getReader will get you a BufferedReader that you can then use to do standard XML parsing.
    – kschneid
    Jun 25, 2012 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.