send XML request string using HTTP POST protocol?

Discussions

XML & Web services: send XML request string using HTTP POST protocol?

  1. I wonder if we can send XML request string using HTTP POST protocol?

    The JSP page needs to send XML request string to another JSP page when the
    user clicks submit button, I wonder how to get started because when user
    click submit button, it will send the form to page2.jsp.

    <form action="page2.jsp" method="POST">
    Name: <input type="text" name="fname">
    //etc.. controls
    <input type="submit">
    </form>
  2. Define a hidden form field to hold your XML data.

    <form action="page2.jsp" method="POST">
    <input type="hidden" name="xmldata">
    Name: <input type="text" name="fname">
    //etc.. controls
    <input type="submit" onsubmit="populate_xmldata()">
    </form>

    Write a JavaScript function [populate_xmldata()] that reads the rest of your form data, creates an XML string and stores it in the xmldata field when the form is submitted.

    In your receiving JSP (page2.jsp), retrieve the XML data from the xmldata parameter:

    <% String xmldata = request.getParameter("xmldata"); %>
  3. Thanks.

    Is it possible just selectively send hidden field "xmldata" when the user click the submit button, instead of submitting all the form control values? Since page2.jsp only cares about field "xmldata."
  4. Thanks. Is it possible just selectively send hidden field "xmldata" when the user click the submit button, instead of submitting all the form control values? Since page2.jsp only cares about field "xmldata."
    In theory yes. To do this, you would have to create a completely separate hidden HTML form. When the user submitted the visible HTML form, you would have to cancel that form submission, transfer the data to the hidden form, and submit that instead.

    Since the target JSP will ignore any extraneous data, I doubt this is worth all the trouble.
  5. Given XSD (xml schema), how to transform the form data to xml string?
  6. JavaScript does not have any support for XML Schema. The easiest thing to do is perform the transformation manually:

    var xml = "<root>";
    xml += "<field1>" + document.theForm.field1.value + "</field1>";
    xml += "<field2>" + document.theForm.field2.value + "</field2>";
    ...
    xml += "</root>";