java - 我还需要做什么/创建什么来使我的 servlet 和 AJAX 工作?

标签 java ajax eclipse tomcat servlets

我创建了一个 AJAX 网络应用程序,它应该将一些数据 (POSITIONS) 发送到我的 servlet,它会在我的服务器中创建一个包含该数据的文本文件。问题是我不知道它是否正常工作,我不知道 web.xml 是如何工作的以及如何为我的应用配置它。

更新:

我遇到了 CORS 问题,但我的 webapp.html 和我的 .war 文件在我的本地主机上。



服务器:Apache Tomcat 7.0
Eclipse EE 火星

提前致谢。

P.S.:我发布下面的片段只是为了观察,但我不能使用脚本,因为我不拥有图书馆所在的企业,如果你想看到它,请检查这个 PostScript 上面的链接。

我的 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);
        }
      });

    });

我的 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();
}

}

和我的 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>

最佳答案

根据屏幕截图,您通过 file:// URL 打开了 HTML 页面。当然,您会碰到 CORS 墙,因为 ajax 请求不是在 file:// URL 上触发的,而是在 http://localhost URL 上触发的。

file:// URL 也修复为完全有值(value)的 http://localhost URL,永远不要使用 file:// URL对于网络资源。将该 source.html 文件移动到项目的公共(public) Web 内容中并通过以下方式打开它:

http://localhost/JqTree/source.html

关于java - 我还需要做什么/创建什么来使我的 servlet 和 AJAX 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248957/

相关文章:

java - 制作并发登录屏幕

javascript - 我的 ajax 跨域请求被 Chrome 取消

java - 我如何将jboss与eclipse集成来运行调试过程

eclipse - Apache Camel : maven-remote-resources-plugin (goal "process") is ignored by m2e

java - 更改参数化测试的名称

java - OpenGL - 颜色布局位置不起作用

javascript - AJAX 表单发送 GET 并将表单发送到当前页面而不是 POST jquery

javascript - `fetch` 请求未显示所需结果,`ajax` 请求显示所需结果

java - 应该为 EE 开发人员安装 Eclipse IDE 还是为 Java 开发人员安装普通 IDE?

java:具有颜色分量和直方图间隔的二维数组