主页 > 编程资料 > Javascript >
发布时间:2016-01-01 作者:网络 阅读:489次

本文实例讲述了jQuery+json实现的简易Ajax调用。分享给大家供大家参考,具体如下:

Userservlet.java代码:

package com.iss.servlet;
import org.json.JSONException;
import org.json.JSONObject;
import com.iss.pojo.User;
import com.iss.util.JSONUtil;
public class UserServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=utf-8");
  //list 添加对象
  List userList = new ArrayList();
  User user1 = new User("张三", "男", "18");
  User user2 = new User("李四", "男", "19");
  User user3 = new User("王五", "男", "20");
  userList.add(user1);
  userList.add(user2);
  userList.add(user3);
  PrintWriter out = response.getWriter();
  String str = null;
  try {
   //帐号密码如果匹配则把list 返回给界面
   if (request.getParameter("userName").equals("jquery")
     && request.getParameter("password").equals("ajax")) {
    str = JSONObject.quote(JSONUtil.toJSONString(userList));
   }
   out.print(str);
  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("str "+str);
  out.flush();
  out.close();
 }
}

Html代码:

帐号 jquery 密码 ajax
  • 帐号
  • 密码
查询到的数据 <>

UserServlet 记得要导入 工具类 JSONStringObject JSONUtil

jsp 要导入 jquery.js和 json.js

希望本文所述对大家jQuery程序设计有所帮助。

关键字词: