美文网首页我爱编程
前端jsp与后端servlet传值(java web)

前端jsp与后端servlet传值(java web)

作者: Echoooo_o | 来源:发表于2018-05-26 17:41 被阅读0次

    jsp中js使用jQuery封装好的 $.ajax()方法与服务器进行交互

      var jsonStick = this.strtoJson();
      $.ajax({
                    type : 'post',//不起作用 要用jsonP
                    url : 'controllerServlet',//要传输数据对象的地址
                    contentType : "application/x-www-form-urlencoded",//转化为form表单传值
                    data : {
                        "js" : jsonStick
                    }, //前面的js是服务端获取的ID 后面的jsonStick是处理后的json对象
                    cache : false,
                    dataType : 'text',//这个关系到你能不能正确接收到servlet 响应的数据
                    async : false, //因为ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false
                    success : function(data) {
                        //console.log(data);
                        flag = data; //采用全局变量flag接收数据(data未作处理)
                    },
                    error : function(msg) { //ajax请求失败后触发的方法
                        //弹出错误信息
                        console.log(msg);
                    }
                });
      //数据处理函数
      function() {
                //'':this.note.name,
                var arr = [];
                var json = {};
                json.note_id = this.id
                json.note = this.text;
                json.left = this.left;
                json.top = this.top;
                json.zindex = this.zIndex;
                if (this.sqlId == null) {
                    this.sqlId = 0;
                }
                json.sqlId = this.sqlId;
                arr.push(json);
                var jsonStick = JSON.stringify(arr);//格式化json数据
                return jsonStick;
            }
    

    servlet接收 处理 响应

    protected void doGet(.........){
      String js = request.getParameter("js");//接收jsp传过来的数据
      JSONArray json = JSONArray.fromObject(js);//对数据进行数组转化
      JSONObject jsonOne = json.getJSONObject(0);//获取确定的json对象
      boolean flag = true;
      PrintWriter out = response.getWriter();
      out.print(flag);//将flag传给jsp
      out.close();
    }
    

    jsp接收响应 并对数据进行处理

      $.ajax({
                type : 'post',
                url : 'sendServlet',
                cache : false,
                dataType : 'json',
                async : false, //因为ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false
                success : function(data) {
                    //console.log(data.result.note)
                    for (var i in data) {
    
                        console.log(data[i])
                        note = new Note();
                        note.id = data[i].note_id;
                        note.text = data[i].note;
                        note.timestamp = new Date().getTime();
                        note.left = data[i].left;
                        note.top = data[i].top;
                        note.zIndex = data[i].zindex;
    
    
                    }
                },
                error : function(msg) { //ajax请求失败后触发的方法
                    //弹出错误信息
                    console.log(msg);
                }
            });
    

    中间用到的jar包以及js

    https://pan.baidu.com/s/1XEOkM1as_10I92WAzooBuA
    密码:gix8
    

    相关文章

      网友评论

        本文标题:前端jsp与后端servlet传值(java web)

        本文链接:https://www.haomeiwen.com/subject/tewmjftx.html