ajax

ajax

ajax
servlet

@WebServlet("/web/DeleteAddressServlet")
public class DeleteAddressServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//从jsp页面取传过来的data
		int id = Integer.parseInt(request.getParameter("id"));
		new AddressService().deleteAddress(id);
	//返还给jsp页面的success的obj或者data  -----response.getWriter()
		response.getWriter().println(
				new Gson().toJson(new ResponseResult<>(1,"ok")));
	}

jsp

function funDelete(id){
		if(confirm("确定删除吗?")){
			$.ajax({
				url:"/TeduStore1/web/DeleteAddressServlet",
				data:"id="+id,  //data是传给servlet的数据
				type:"get",
				dataType:"json",
				successs:function(obj){//接受的response.getWriter()
					alert(obj.message);					
				}
			});

(返还是个数组)地址有多个的时候

servlet

@WebServlet("/web/ShowAddressServlet")
public class ShowAddressServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     HttpSession session = 
	    		 request.getSession();
	     User user = (User)session.getAttribute("user");
	     List<Address> list = new AddressService().
	          getAddressByUid(user.getId());
		
	     ResponseResult<List<Address>> rr = 
	    		 new ResponseResult<>(1,"ok");
	     rr.setData(list);
	     //返还给jsp页面的东西response.getWriter(),success的obj
		response.getWriter().print(new Gson().toJson(rr));
	}

jsp

function showAddress(){
		$.ajax({
			url:"/TeduStore1/web/ShowAddressServlet",
			data:id=$aa.value(),
			type:"get",
			dataType:"json",
			success:function(obj){	
				
				var header = '<div class="aim_title">'+
                '<span class="dzmc dzmc_title">地址名称</span><span class="dzxm dzxm_title">姓名</span><span class="dzxq dzxq_title">地址详情</span><span class="lxdh lxdh_title">联系电话</span><span class="operation operation_title">操作</span>'+
                '</div>';
                $(".address_information_manage").html(header);
                
                for(i = 0;i<obj.data.length;i++){
				
                if(obj.data[i].isDefault==1){
	                var str1 = '<div class="aim_content_one aim_active">'+
	                    '<span class="dzmc dzmc_active">'+obj.data[i].addressName+'</span>'+
	                    '<span class="dzxm dzxm_normal">'+obj.data[i].recvName+'</span>'+
	                    '<span class="dzxq dzxq_normal">'+obj.data[i].recvDistrict+'</span>'+
	                    '<span class="lxdh lxdh_normal">'+obj.data[i].recvMobile+'</span>'+
	                    '<span class="operation operation_normal">'+
	                    	'<span class="aco_change" οnclick="funUpdate('+obj.data[i].id+');">修改</span>|<span class="aco_delete" οnclick="funDelete('+obj.data[i].id+');">删除</span>'+
	                    '</span>'+
	                    '<span class="swmr swmr_normal" id="'+obj.data[i].id+'"></span>'+
	                '</div>';
	                $(".address_information_manage").append(str1);
                }

例子:(看)

用jquery的 ajax回调函数 返回的值就是 print中的字符串
action:
getResponse().getWriter().print("添加失败,工号已经存在!");

js:
$(function(){
 $.ajax({
async : true,
type : "POST",
url :'url地址',
success : function(data) {
alert(data);
}
上一篇:response.getWriter.print()出现中文乱码的解决方法


下一篇:大话HTTP协议漫画+图解打造高中生也能学会的编程基础课程