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

 更新时间:2018年01月22日 15:11:32   作者:星空下的那个人影  
下面小编就为大家分享一篇浅谈ajax在jquery中的请求和servlet中的响应,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在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中的响应就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用js显示当前时间示例

    使用js显示当前时间示例

    这篇文章主要介绍了使用js显示当前时间示例,这里也用到了jquery,让得把它也要引入到页面中
    2014-03-03
  • jquery中$(#form :input)与$(#form input)的区别

    jquery中$(#form :input)与$(#form input)的区别

    本节为大家介绍下jquery 中$(#form :input)与$(#form input)的区别,需要的朋友可以参考下
    2014-08-08
  • jQuery使用之标记元素属性用法实例

    jQuery使用之标记元素属性用法实例

    这篇文章主要介绍了jQuery使用之标记元素属性用法,实例分析了jQuery如何控制页面,包含元素的属性、css样式风格、DOM模型、表单元素和事件处理等使用技巧,需要的朋友可以参考下
    2015-01-01
  • 基于easyui checkbox 的一些操作处理方法

    基于easyui checkbox 的一些操作处理方法

    下面小编就为大家带来一篇基于easyui checkbox 的一些操作处理方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • jQuery jcrop插件截图使用方法

    jQuery jcrop插件截图使用方法

    项目需要做一个头像截取的功能,类似于QQ头像截取功能。在网上搜了下,最后使用jQuery插件jcrop来截取
    2013-11-11
  • 谈谈jQuery之Deferred源码剖析

    谈谈jQuery之Deferred源码剖析

    这篇文章主要介绍了谈谈jQuery之Deferred源码剖析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • jQuery插件 tabBox实现代码

    jQuery插件 tabBox实现代码

    最近对js以及jq产生了浓厚的兴趣,看到jq有很多很好用的插件,功能各异,包括webUI,jqGrid等等。心里萌发了制作属于自己的jq插件的想法。
    2010-02-02
  • 基于jQuery实现Tabs选项卡自定义插件

    基于jQuery实现Tabs选项卡自定义插件

    这篇文章主要为大家详细介绍了基于jQuery实现Tabs选项卡自定义插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • jQuery实现移动 和 渐变特效的点击事件

    jQuery实现移动 和 渐变特效的点击事件

    这里给大家分享的是一则使用jQuery实现移动和渐变特效的点击事件效果,非常简单实用,这里推荐给大家。
    2015-02-02
  • jquery 插件学习(三)

    jquery 插件学习(三)

    用惯jquery的用户可能习惯于连写行为,也就是说在调用一个方法之后,紧跟着调用另一个方法,如此连写不断,形成一个珍珠链,而且编写灵活,方便
    2012-08-08

最新评论