-1

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>
Kyle
  • 1,568
  • 2
  • 20
  • 46
  • "The problem is I don't know whether it's working or not". Maybe first you should define what are your criteria for success, so you know when you've achieved them. – JP Moresmau Jul 06 '15 at 14:58
  • What's the problem you're facing? Why can't you determine if what you have works or not? – mabi Jul 06 '15 at 14:58
  • 1
    you've already posted that code a couple of days ago, and people have already pointed out your url is wrong. Your servlet is under the url-pattern /Hello, so your url should end in /Hello. Maybe /JqTree/Hello? Type in the url in the browser to make sure you don't get a 404 response. – JP Moresmau Jul 06 '15 at 15:00
  • Just doesn't create the positions.txt, the servlet works because I've tested it before, but I'm not sure about the `web.xml` and my `AJAX` web app, and I don't know how to check them up to see whether they're working or not – Kyle Jul 06 '15 at 15:01

1 Answers1

1

As per the screenshot, you opened the HTML page by a file:// URL. Of course you will hit the CORS wall as the ajax request is not fired on file:// URL, but on http://localhost URL.

Fix the file:// URL to be a fullworthy http://localhost URL too and never use file:// URLs for web resources. Move that source.html file into public web content of the project and open it via:

http://localhost/JqTree/source.html

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So that way: http://localhost/JqTree/source.html, I'm gonna be able to access it the way I'm doing right now? which means I need to put my source in the `webapps` folder then? – Kyle Jul 07 '15 at 12:17
  • In `WebContent` folder of your Eclipse project. – BalusC Jul 07 '15 at 12:30
  • oh, okay I'm gonna do it right now, thanks for helping me BalusC :D – Kyle Jul 07 '15 at 12:31
  • I just tested it, but I've got the same thing : http://i.stack.imgur.com/wE3hZ.png Could you tell me why? – Kyle Jul 07 '15 at 12:34
  • Because the port is different. You've apparently 2 separate servers? What exactly is the intent? Or did you in the meanwhile modify the JavaScript code to hardcode `http://localhost/`? My answer was based on the code in the question in its current form. – BalusC Jul 07 '15 at 12:36
  • It shows that host and origin are using the same ports, can you point it out, I'm sorry for my blindness :/ – Kyle Jul 07 '15 at 12:39
  • Browser's address bar shows port 8080. Second POST (the one in red) shows no port (thus default port of 80 is assumed). – BalusC Jul 07 '15 at 12:39
  • okay I will change it right now I'm sorry for bothering you, and a huge thank you – Kyle Jul 07 '15 at 12:44
  • I set everything to 8080 8005 and 8009 but my servlet `JqTree` is still `80` how can I change it, my `server.xml` is already right, I rebooted my server, but my servlet is still 80, even though I exported and deployed it again – Kyle Jul 07 '15 at 13:11
  • Eclipse won't use Tomcat's own `server.xml` when you let Eclipse manage Tomcat. Instead, doubleclick Tomcat server in Eclipse's *Servers* view and edit there. – BalusC Jul 07 '15 at 13:13
  • I already did it in eclipse, I want to run it on my server not on "eclipse" anymore – Kyle Jul 07 '15 at 13:15
  • How did you install Tomcat? Just downloaded the ZIP and extracted it? Or did you download the EXE containing the Windows service? If the EXE, then read this: http://stackoverflow.com/q/5064733 – BalusC Jul 07 '15 at 13:17
  • I downloaded the EXE and installed, it works, I use the /manage thing and stuff, my problem is my servlet that appears as 80, Okay – Kyle Jul 07 '15 at 13:19
  • As explained in the link, you should not be using the EXE for development. You will end up with two instances of Tomcat, whereby the Windows service one is always booted during Windows startup and permanently running. The one which you manage via Eclipse does not refer that instance but a completely separate one. The Windows service makes no sense during development. It's intented for production environments only. Uninstall it. – BalusC Jul 07 '15 at 13:21
  • okay I'm gonna do it right now. and my servlet why is it appearing with `80`? – Kyle Jul 07 '15 at 13:22
  • I've uninstalled the java one and how do I fix my servlet now, should I "create" it again? – Kyle Jul 07 '15 at 13:31
  • Didn't you had that just in your Eclipse project? – BalusC Jul 07 '15 at 13:32
  • nope, that was happening with war file with my page in the `webcontent` folder, after I deployed that on my "windows" tomcat server, do you want pictures? – Kyle Jul 07 '15 at 13:35
  • So you initially deployed the very same project to two different servers? This doesn't make sense. Just stick to Eclipse-integrated Tomcat. Rightclick it and add the project. Then start it. Then enter the page URL in your favourite webbrowser. – BalusC Jul 07 '15 at 13:37
  • so keep the eclipse EE one and uninstall the tomcat on my machine? but how do I know that now I have only one server running/installed – Kyle Jul 07 '15 at 13:38
  • Uninstall Windows service. Just get Tomcat as ZIP file and extract it and integrate that in Eclipse instead. – BalusC Jul 07 '15 at 13:39
  • I'm so close, but I'm still getting `80`: http://i.stack.imgur.com/bejjo.png, Why is it? – Kyle Jul 07 '15 at 13:58
  • Well, this suggests you still have a hardcoded `http://localhost/` URL in JavaScript. – BalusC Jul 07 '15 at 14:04
  • That's what I'm thinking, What do you suggest? Is it related to my ajax call maybe? – Kyle Jul 07 '15 at 14:07
  • it says that I don't have anything on my ´localhost/80´, it doesn't exist – Kyle Jul 07 '15 at 14:19
  • I just deleted the whole project and "created" it again, worked! – Kyle Jul 07 '15 at 14:33