使用Ajax和JSP将表单保存在XML文件中

时间:2022-01-29 09:56:44

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:

我想创建一个带有名称和电子邮件的简单表单,并将这些数据保存在XML文件中。到目前为止,我发现使用Ajax和jQuery非常容易。所以我使用了通常的代码:

//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.

如果我理解得很好,我应该在url中添加将要创建的XML文件的名称。

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.

当用户单击一个按钮时,我使用Ajax请求调用该函数,然后我应该在某处调用一个函数来生成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.

我也用两个豆子。一个用于设置用户的元素,另一个用于保存XML中的数据。我正在使用xStream库作为xml,虽然我不知道是否是最好的解决方案。

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

现在的问题是我无法将所有这些连接在一起以便将数据保存在XML中。

Does anyone know what should I do?

有谁知道我该怎么办?

Thanks a lot!

非常感谢!

1 个解决方案

#1


1  

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.

对不起,但您的理解不正确。 url应指向处理HTTP POST请求的服务器端CGI / API的URL。 dataType表示在处理HTTP请求之后从服务器端返回的HTTP响应的数据类型。这可以是HTML字符串,JSON字符串或XML字符串。为了便于立即处理响应,您可以在其中设置数据类型,以便jQuery知道如何“预格式化”响应。通常使用JSON,因为它是最简洁,最快速的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:

对于JSP / Servlet应用程序,您需要让url指向一个Servlet,该Servlet具有如下实现的doPost()方法:

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.
}

#1


1  

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.

对不起,但您的理解不正确。 url应指向处理HTTP POST请求的服务器端CGI / API的URL。 dataType表示在处理HTTP请求之后从服务器端返回的HTTP响应的数据类型。这可以是HTML字符串,JSON字符串或XML字符串。为了便于立即处理响应,您可以在其中设置数据类型,以便jQuery知道如何“预格式化”响应。通常使用JSON,因为它是最简洁,最快速的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:

对于JSP / Servlet应用程序,您需要让url指向一个Servlet,该Servlet具有如下实现的doPost()方法:

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.
}