从jsp页面发送ajax请求,servlet接受参数并返回json数据

<span style="font-family:Arial, Helvetica, sans-serif;background-color:rgb(255,255,255);">博主注册了CSDN好久了,一直没写过(哈哈)</span>


今天遇到了问题把我难住了,解决之后就赶紧来记下来

这是一个很简单的更新用户的问题

先来看看项目所需jar包


接下来就是jsp页面的东西,ajax发送id

    function editCustomer(id) {  
                $.ajax({  
                    type:"get",  
                    url:"${pageContext.request.contextPath }/editStudioServlet",  
                    data:{"id":id},   
                    success:function(data) {  
                        $("#edit_cust_id").val(${data.cust_id});  
                        $("#edit_customerName").val(data.cust_name);  
                        $("#edit_phone").val(data.cust_phone);  
                        $("#edit_mobile").val(data.cust_mobile);   
                        alert(data);  
                          
                    }  
                });  
            }  

然后在servlet接受参数,并去数据库查询结果

    String id = request.getParameter("id");  
            //System.out.println(id);  
            StudioDao dao = new StudioDaoImpl();  
            Customer studio = null;  
            try {  
                studio = dao.getCustomerById(Integer.parseInt(id));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            Gson gson = new Gson();  
            String json = gson.toJson(studio);  
            response.setCharacterEncoding("UTF-8");    
            response.setContentType("application/json; charset=utf-8");    
            PrintWriter writer = response.getWriter();  
            writer.append(json);  
然后Ajax的回调函数将从servlet发送过来的json数据填入input框中完成。

最后贴一张图


好了,大功告成,以后博主要勤写文章。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值