【问题标题】:How do i get JSON object into servlet?如何将 JSON 对象放入 servlet?
【发布时间】:2015-07-10 20:19:38
【问题描述】:

基本上我有一个 HTML 文件,我在其中创建了一个 JSON 对象:

    function CreateJson() {
        var tableObj = [];

        var loopCounter = 0;
        var inputValues = [];

        var table = document.getElementById('inputTable');
        for (var r = 0, n = table.rows.length; r < n; r++) {
            for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                loopCounter++;

                inputValues.push(table.rows[r].cells[c].firstChild.value);

                if (loopCounter == 3) {
                    tableObj.push({
                        model : inputValues[0],
                        colour : inputValues[1],
                        year : inputValues[2]
                    });
                    loopCounter = 0;
                    inputValues = [];
                }
            }
        }
        $.ajax({
            type : 'POST',
            dataType : 'json',
            data: { tableObj : JSON.stringify(jsondata)},
            url : 'ServletUrl',
            timeout : 5000,
            success : function(data, textStatus) {
                // whatever
            },
            error : function(xhr, textStatus, errorThrown) {
                // whatever
            }
        });
    }

我的 Java servlet 有一个带有 action 的表单,当我提交表单时,上面的 JavaScript 代码就会被执行。如何检索和解析 servlet 中的 JSON 对象? JSON 对象为 tableObj

编辑: 我编辑了我的 CreateJson 函数:

在我的 servlet 中我有:

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

        Object json = request.getParameter("tableObj");
        System.out.println(json);

在日志中它打印 null。

【问题讨论】:

  • 我不明白你的问题。 TableObj 是一个 json 对象?你想在 servlet 中得到它吗?你送的?
  • 如何将 tableObj 发送到 servlet。我想在我的 servlet 中使用 tableObj
  • 您是否尝试将其发布到 servlet,例如使用 jquery? api.jquery.com/jquery.post
  • 你实现了一些 servlet 代码吗?请向我们展示以便做出准确的回答
  • 要么将 $.ajax() 类型更改为“GET”,要么将 doGet() 中的相同代码复制并粘贴到 servlet 中的 doPost() 中。

标签: javascript java json servlets


【解决方案1】:

看到你在你的 java 脚本中使用 POST

$.ajax({
            type : 'POST',
            dataType : 'json',
            data: { tableObj : JSON.stringify(jsondata)},
            url : 'ServletUrl',
            timeout : 5000,
            success : function(data, textStatus) {
                // whatever
            },
            error : function(xhr, textStatus, errorThrown) {
                // whatever
            }
        });

但是你没有在你的servlet中使用相同的动词(doPost),请改变

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException { 

【讨论】:

  • 您不能在 Servlet 实现中将 doGet() 更改为 doPost()。您必须声明这两种方法。
  • 我想说的是hi正在读取doGet中的值,而不是他必须在doPost中读取它,你是对的,这两种方法都属于同一个接口......跨度>
  • 我将其更改为 type : 'GET',但我的 doGet 函数仍然返回 null。
猜你喜欢
  • 1970-01-01
  • 2014-10-15
  • 2021-08-22
  • 1970-01-01
  • 2013-07-26
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多