Ajax:jsp页面
- <script language="javascript">
- var xmlHttp;
- //创建XMLHttpRequest对象
- function createHttpRequest() {
- if (window.ActiveXObject) {//如果可以取得ActiveXObject
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//Internet Explorer
- } else if (window.XMLHttpRequest) {//如果可以取得XMLHttpRequest
- xmlHttp = new XMLHttpRequest();//Mozilla、Firefox、Safari
- }
- }
-
- function Check(){
- createHttpRequest();
- //将状态触发器绑定到一个函数
- xmlHttp.onreadystatechange = processor;//处理状态改变函数
- // xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
-
- 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;
- //通过get方法向指定的URL即Servlet对应URL建立服务器的调用
- xmlHttp.open("post", url, true);
- //发送请求
- xmlHttp.send(null);
-
- }
- function processor() {
- var responseContext;
- //如果响应完成
- if (xmlHttp.readyState == 4) {
- //如果返回成功
- if (xmlHttp.status == 200) {
- //取得响应内容
-
- responseContext = xmlHttp.responseText;
- //如果检查有效
-
- switch(parseInt(responseContext)){
- case 0: document.getElementById("khinfo").innerHTML="卡号不存在";
- break;
- case 1:document.getElementById("khinfo").innerHTML="卡号已停用";
- break;
- case 2:document.getElementById("mminfo").innerHTML="密码错误";
- break;
- case 3:document.getElementById("jyjeinfo").innerHTML="余额不足";
- break;
- case 4:alert("交易成功");window.location.href="<%=basePath%>servlet/ZhxxServletSearch";
- break;
-
- }
- }
- }
- }
servlet:
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- request.setCharacterEncoding("utf-8");
- PrintWriter out = response.getWriter();
- CommJdbcManager comm = new CommJdbcManager();
- String jyjeStr = request.getParameter("jyje");
- BigDecimal jyje = new BigDecimal(jyjeStr);
- String mm = request.getParameter("mm");
- String kh = request.getParameter("kh");
- String jylx = request.getParameter("jylx");
- String bz = request.getParameter("bz");
- String date = NewDate.dateFormat();
- String sqlSearch = "select * from p_zhxx where kh='" + kh + "'";
- Zhxx zhxx = null;
- HttpSession session = request.getSession();
- LoginBean operator = (LoginBean) session.getAttribute("user_info");
- String[] sql = new String[2];
- String yhbh = operator.getYhbh();
- try {
- zhxx = (Zhxx) comm.findForObject(sqlSearch, Zhxx.class);
- if (zhxx != null) { // 账户存在
- if (zhxx.getZhzt().equals("0")) {
- out.println("1");// 停用
- return;
- }
- if (jylx.equals("2")) { // 取款
- if (!zhxx.getMm().equals(mm)) {
- out.println("2");// 密码错误
- return;
- }
- BigDecimal zhye = zhxx.getZhye();
- if (zhye.compareTo(jyje)< 0) {
- out.println("3"); // 余额不足
- return;
- }
- sql[0] = "update p_zhxx set ZHYE=ZHYE-'" + jyje
- + "' where KH='" + kh + "'";
- sql[1] = "insert into p_jyls(YHBH,ZHBH,JYRQ,JYLX,JYJE,BZ)values('"
- + yhbh
- + "','"
- + zhxx.getZhbh()
- + "','"
- + date
- + "','" + jylx + "','" + jyje + "','" + bz + "')";
- comm.executeSql(sql);
- } else { // 存款
- sql[0] = "update p_zhxx set ZHYE=ZHYE+'" + jyjeStr
- + "' where KH='" + kh + "'";
- sql[1] = "insert into p_jyls(YHBH,ZHBH,JYRQ,JYLX,JYJE,BZ)values('"
- + yhbh
- + "','"
- + zhxx.getZhbh()
- + "','"
- + date
- + "','" + jylx + "','" + jyje + "','" + bz + "')";
- comm.executeSql(sql);
- }
- out.println("4");// 成功
- return;
- } else {
- out.println("0");// 卡号不存在
- }
- out.flush();
- out.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
Ajax是在浏览器中运行的,与语言无关。
参考文章:全面剖析XMLHttpRequest对象
http://hi.baidu.com/zhangw19811010/blog/item/8f632d23150bac4e925807fc.html
阅读(1499) | 评论(0) | 转发(1) |