欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈ajax在jquery中的请求和servlet中的响应

程序员文章站 2022-04-29 12:09:23
在jsp中,首先,你需要导入jquery的架包: 获取可返回站点的根路径: <% string path = request.getcontex...

在jsp中,首先,你需要导入jquery的架包:

获取可返回站点的根路径:

<% 
  string path = request.getcontextpath(); 
%> 

在jquery中写ajax请求:

<script type="text/javascript">
     $(function(){
        $(".b").click(function(){
        $.ajax({
            type: "get",
                     //对应servlet中的方法
            url: "<%=path%>" + "/queryevaluatebyuserid.do",
                     //返回是json数据
            datatype: "json",
            async:false,
           data:{
            },
            success: function(data){
              str = "";
               if(data != null){
                               //循环表单列表
                 for (var i in data)
                  {
                     var num = parseint(i) + 1 ;                         
                    str +="<tr><td>" + num + "</td><td>" 
                    + data[i]['name'] + "</td><td>"
                    + data[i]['price'] + "元</td>" 
                    + "</tr>";
                  }
                 $(".trtd4").after(str);
               }else{
                 
               }
               
            },
            error: function(data){
            }
          }) 
      });
     }
</script>

jsp部分:

<div class="tab-pane" id="b" style="text-align:center;"> 
          <div class="row marg" > 
            <table border="2 " style="width:80%;text-align:center;"> 
              <tr class="trtd4"> 
                <th>序号</th> 
                <th>业主名</th> 
                <th>金额</th> 
              </tr>              
            </table> 
          </div> 
        </div> 

在servlet中用到了阿里巴巴的快速转换json的包com.alibaba.fastjson.json:

private void queryevaluatebyuserid(httpservletrequest request, httpservletresponse response) throws sqlexception, ioexception, servletexception{
			httpsession session=request.getsession();
			request.setcharacterencoding("utf-8");
			response.setcontenttype("text/html");
			response.setcharacterencoding("utf-8");
			cookie[] cookies = request.getcookies();
			int ownerid = 0;
			for (int i = 0; i < cookies.length; i++) {
	      cookie cookie = cookies[i];
	      if (cookie.getname().equals("ownerid")) {
	      	ownerid = integer.parseint(cookie.getvalue()); 
	      }
			}
			list<order> orderlist = new arraylist<>();
			list<evaluate> queryevaluatelist = new arraylist<>();
			orderlist = orderserviceimpl.queryorderlist(ownerid, null, null, null, null, null);
			list<map<string, string>> workers = new arraylist<map<string, string>>(); 
			for(int i = 0;i < orderlist.size();i++){
				map<string,string> order = new hashmap<string, string>();
				order.put("description", orderlist.get(i).getdescription());
				order.put("name", orderlist.get(i).getownername());
				system.out.println(orderlist.get(i).getdescription());
				order.put("type",orderlist.get(i).gettypename());
				queryevaluatelist = orderserviceimpl.queryevaluatelistbyuserid(orderlist.get(i).getid());
				order.put("comment", queryevaluatelist.get(0).getcomment());
				list<allocation> allocation = orderserviceimpl.queryallocationbyorderid(orderlist.get(i).getid());
				order.put("price", string.valueof(allocation.get(0).getprice()));
				 system.out.println(order);
				workers.add(order);
			}
            //将map键值对转换成json,传给jsp
            response.getoutputstream().write(json.tojsonbytes(workers));
		}

以上这篇浅谈ajax在jquery中的请求和servlet中的响应就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。