如何让我的网站与我的servlet通信?

时间:2015-06-29 14:38:06

标签: java html ajax html5 servlets

我创建了一个获取JqTree"坐标"并将这些坐标var POSITIONS发送到我的服务器,我的servlet将创建一个包含"坐标"的文件,然而,我不知道我怎么回事进行此沟通。你能帮帮我吗?

提前致谢。

我在下面做了一个图表来解释我的想法。

1 - 用户更改树 enter image description here

2 - 此代码会立即获得这些职位

$(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://MyServer', {
            tree: $(this).tree('toJson')
        });
        alert("done"); //this will post the json of the latest tree structure.
    }
);


});

3 - 然后它应该由AJAX发送

$(function() {
alert("file has been successfully sent");
var data = new FormData();
data.append("custom_css", POSITIONS);
$.ajax({
    url: 'myserver',
    type: 'POST',
    data: data,
    cache: false,
    dataType: 'json',
    processData: false,
    contentType: false,
    success: function(response) {
        alert("file has been successfully sent");
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert('ERRORS: ' + textStatus);
    }
});

});

4 - 最后由我的servlet收到并保存到文本文件中: Positions.txt

package com.srccodes.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
* Servlet implementation class HelloWorld
*/
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public HelloWorld() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter printWriter  = response.getWriter();
    printWriter.println("<h1>Hello World!</h1>");

    String position = "JQTREE POSITIONS";
    PrintWriter writer = new PrintWriter("Positions.txt", "UTF-8");
    writer.println(position);
    writer.close();
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

1 个答案:

答案 0 :(得分:2)

  
      
  1. 您的.ajax网址应与@WebServlet()映射匹配。在您的情况下"/HelloWorld"
  2.   
  3. 由于您POST数据,您应该覆盖servlet中的doPost()方法。
  4.   
  5. 最后在您的doPost()方法中,从"custom_css"变量中检索数据,例如.. request.getParameter("custom_css")
  6.   
  7. 继续执行其余的业务逻辑。
  8.   
$.ajax({
    url: '/HelloWorld',
    type: 'POST',
    data: data,
    cache: false,
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getParameter("custom_css")

// Proceed with your business logic here, using above data
//
//
//
//
}