Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2222870
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: Java

2012-07-20 00:06:56

Ajax:jsp页面

点击(此处)折叠或打开

  1. <script language="javascript">
  2. var xmlHttp;
  3. //创建XMLHttpRequest对象
  4.     function createHttpRequest() {
  5.         if (window.ActiveXObject) {//如果可以取得ActiveXObject
  6.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//Internet Explorer
  7.         } else if (window.XMLHttpRequest) {//如果可以取得XMLHttpRequest
  8.             xmlHttp = new XMLHttpRequest();//Mozilla、Firefox、Safari
  9.         }
  10.     }
  11.     
  12.     function Check(){
  13.         createHttpRequest();
  14.         //将状态触发器绑定到一个函数
  15.         xmlHttp.onreadystatechange = processor;//处理状态改变函数
  16.     //    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  17.         
  18.         var url ="<%=basePath%>servlet/CxywServletAjax?kh="+document.thisform.kh.value+"&mm="+document.thisform.mm.value+"&jylx="+document.thisform.jylx.value+"&jyje="+document.thisform.jyje.value;
  19.         //通过get方法向指定的URL即Servlet对应URL建立服务器的调用
  20.         xmlHttp.open("post", url, true);
  21.         //发送请求            
  22.         xmlHttp.send(null);
  23.         
  24.     }
  25.     function processor() {
  26.         var responseContext;
  27.         //如果响应完成
  28.         if (xmlHttp.readyState == 4) {
  29.             //如果返回成功
  30.             if (xmlHttp.status == 200) {
  31.                 //取得响应内容
  32.                 
  33.                 responseContext = xmlHttp.responseText;
  34.                 //如果检查有效
  35.                 
  36.                 switch(parseInt(responseContext)){
  37.                 case 0: document.getElementById("khinfo").innerHTML="卡号不存在";
  38.                 break;
  39.                 case 1:document.getElementById("khinfo").innerHTML="卡号已停用";
  40.                 break;
  41.                 case 2:document.getElementById("mminfo").innerHTML="密码错误";
  42.                 break;
  43.                 case 3:document.getElementById("jyjeinfo").innerHTML="余额不足";
  44.                 break;
  45.                 case 4:alert("交易成功");window.location.href="<%=basePath%>servlet/ZhxxServletSearch";
  46.                 break;
  47.                 
  48.                 }
  49.             }
  50.         }
  51.     }

servlet:

点击(此处)折叠或打开

  1. public void doPost(HttpServletRequest request, HttpServletResponse response)
  2.             throws ServletException, IOException {

  3.         response.setContentType("text/html");
  4.         request.setCharacterEncoding("utf-8");
  5.         PrintWriter out = response.getWriter();
  6.         CommJdbcManager comm = new CommJdbcManager();
  7.         String jyjeStr = request.getParameter("jyje");
  8.         BigDecimal jyje = new BigDecimal(jyjeStr);
  9.         String mm = request.getParameter("mm");
  10.         String kh = request.getParameter("kh");
  11.         String jylx = request.getParameter("jylx");
  12.         String bz = request.getParameter("bz");
  13.         String date = NewDate.dateFormat();
  14.         String sqlSearch = "select * from p_zhxx where kh='" + kh + "'";
  15.         Zhxx zhxx = null;
  16.         HttpSession session = request.getSession();
  17.         LoginBean operator = (LoginBean) session.getAttribute("user_info");
  18.         String[] sql = new String[2];
  19.         String yhbh = operator.getYhbh();
  20.         try {
  21.             zhxx = (Zhxx) comm.findForObject(sqlSearch, Zhxx.class);
  22.             if (zhxx != null) { // 账户存在

  23.                 if (zhxx.getZhzt().equals("0")) {
  24.                     out.println("1");// 停用

  25.                     return;
  26.                 }
  27.                 if (jylx.equals("2")) { // 取款

  28.                     if (!zhxx.getMm().equals(mm)) {
  29.                         out.println("2");// 密码错误

  30.                         return;
  31.                     }
  32.                     BigDecimal zhye = zhxx.getZhye();
  33.                     if (zhye.compareTo(jyje)< 0) {
  34.                         out.println("3"); // 余额不足

  35.                         return;
  36.                     }

  37.                     sql[0] = "update p_zhxx set ZHYE=ZHYE-'" + jyje
  38.                             + "' where KH='" + kh + "'";
  39.                     sql[1] = "insert into p_jyls(YHBH,ZHBH,JYRQ,JYLX,JYJE,BZ)values('"
  40.                             + yhbh
  41.                             + "','"
  42.                             + zhxx.getZhbh()
  43.                             + "','"
  44.                             + date
  45.                             + "','" + jylx + "','" + jyje + "','" + bz + "')";
  46.                     comm.executeSql(sql);
  47.                 } else { // 存款

  48.                     sql[0] = "update p_zhxx set ZHYE=ZHYE+'" + jyjeStr
  49.                             + "' where KH='" + kh + "'";
  50.                     sql[1] = "insert into p_jyls(YHBH,ZHBH,JYRQ,JYLX,JYJE,BZ)values('"
  51.                             + yhbh
  52.                             + "','"
  53.                             + zhxx.getZhbh()
  54.                             + "','"
  55.                             + date
  56.                             + "','" + jylx + "','" + jyje + "','" + bz + "')";
  57.                     comm.executeSql(sql);
  58.                 }
  59.                 out.println("4");// 成功

  60.                 return;
  61.             } else {
  62.                 out.println("0");// 卡号不存在

  63.             }
  64.             out.flush();
  65.             out.close();
  66.         } catch (Exception e) {
  67.             // TODO Auto-generated catch block

  68.             e.printStackTrace();
  69.         }

  70.     }

Ajax是在浏览器中运行的,与语言无关。

参考文章:全面剖析XMLHttpRequest对象
http://hi.baidu.com/zhangw19811010/blog/item/8f632d23150bac4e925807fc.html

阅读(1496) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~