I created an AJAX web app that should send some data (POSITIONS
) to my servlet that would create a text file in my server containing that data. The problem is I don't know whether it's working or not, and I don't know how the web.xml
works and how to configure it for my app.
UPDATE:
I'm getting that CORS problem, but my webapp.html and my .war file are at my localhost.
Server: Apache Tomcat 7.0
Eclipse EE Mars
Thanks in advance.
P.S.: I posted the snippet below only for observation, but I can't use script because I don't own the businesses where the libraries are, if you want to see it check it on the link above this PostScript.
My AJAX
$(document).ready(function() {
var POSITIONS;
//var data is a dynamic JSON file that should be created in the backend.
var data = [{
label: 'node1',
id: 1,
children: [{
label: 'child1',
id: 2
}, {
label: 'child2',
id: 3
}]
}, {
label: 'node2',
id: 4,
children: [{
label: 'child3',
id: 5
}]
}];
$('#tree1').tree({
data: data,
autoOpen: true,
dragAndDrop: true
});
console.log($('#tree1').tree('toJson')); //This will give you the loading jqtree structure.
$('#tree1').bind(
'tree.move',
function(event) {
event.preventDefault();
// do the move first, and _then_ POST back.
event.move_info.do_move();
console.log($(this).tree('toJson')); //this will give you the latest tree.
POSITIONS = $(this).tree('toJson');
alert(POSITIONS);
$.post('http://sistema.agrosys.com.br/sistema/labs/CSS_HTML/', {
tree: $(this).tree('toJson')
});
alert("done"); //this will post the json of the latest tree structure.
}
);
var data = new FormData();
data.append("JqTree", POSITIONS);
alert('Sending: ' + POSITIONS);
$.ajax({
url: '/JqTree/Hello',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
success: function(response) {
alert("file has been successfully sent\n\n" + POSITIONS);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('ERRORS: ' + textStatus);
}
});
});
My Servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html><body>");
out.print("<h3>Hello Servlet</h3>");
out.print("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String position = request.getParameter("JqTree");
PrintWriter writer = new PrintWriter("Positions.txt", "UTF-8");
writer.println(position);
writer.close();
}
}
and my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JqTree</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Hello</display-name>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>