tags:

views:

169

answers:

2

Hello, I want to create a simple form with a name and an email and save these data in an XML file. So far I found that using Ajax with jQuery is quite easy. So I used the usual code:

//dataString have the values taken from the form
var dataString = 'name='+ name + '&email=' + email;
$.ajax({
 type: "POST",
 url: "users.xml",
 data: dataString,
 dataType: "xml",
 success: function() { .... }
});

If I understood well, in the url I should add the name of the XML file that will be created.

When the user clicks a button I call the function with the Ajax request, and then I should call somewhere a function for generating the xml.

I am using also two beans. One is for setting the elements of the user and the other is for saving the data in the XML. I am using the XStream library for the xml although I don't know if is the best solution.

The problem now it that I can not connect all these together in order to save the data in the XML.

Does anyone know what should I do?

Thanks a lot!

+1  A: 

Sorry, but your understanding is incorrect. The url should point to the URL of a server-side CGI/API which processes the HTTP POST request. The dataType indicates the data type of the HTTP response which is been returned from the server side after processing the HTTP request. This can be either a HTML string, a JSON string or a XML string. To ease immediate processing of the response, you can set the datatype there so that jQuery knows how to "preformat" the response. Often JSON is been used since it's the most compact and the quickiest to process in Javascript.

In case of a JSP/Servlet application, you need to let the url point to a Servlet which has the doPost() method implemented like follows:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // First process HTTP request.
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    // Do your business stuff here. You want to store this in a xml file? 

    // Then return HTTP response.
    response.setContentType("text/xml");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write("<status>ok</status>"); // Or whatever XML string you would like to return depending on the outcome of the business stuff.
}
BalusC
A: 

Thanks for your reply. As I am quite new to these technologies could you help a bit more? These are the beans I have:

<jsp:directive.page import="xmlParsing.Customer"/>
<jsp:useBean id="customer" class="xmlParsing.Customer" scope="session"/> 
<jsp:setProperty name="customer" property="*"/> 

<jsp:directive.page import="xmlParsing.GenerateXml"/>
<jsp:useBean id="xml" class="xmlParsing.GenerateXml" scope="session"/> 
<jsp:setProperty name="xml" property="*"/> 

So you mean in the url to have: "xmlParsing.GenerateXml" ? In this file (GenerateXml.java) I have the doPost(). But where should I give the name of the xml file? Maybe I should also something in the success? Add for example there the calling of this function?

Thanks again!

novellino