关闭

利用Ajax返回用户ID是否已被使用的检测信息

1026人阅读 评论(1) 收藏 举报
判断用户ID是否已被使用,若被使用则显示是否要修改的提示信息。
 
------------- AjaxTest.jsp ---------------------------------------------------------------
<%@ page contentType="text/html; charset=GB2312" %>

<html>
<head>
<title>Ajax Test</title>
<SCRIPT Language=javaScript><!--

function AJAXInteraction(url) {
  this.url = url;
  var req = init();
  req.onreadystatechange = processRequest;

function init() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function processRequest () {
  if (req.readyState == 4) {
    if (req.status == 200) {
      //alert(req.responseText);
      postProcess(req.responseXML);
    }
  }
}

  this.send = function() {
    req.open("GET", url, true);
    req.send(null);
  }
}

function postProcess(responseXML) {
  var existUserID_tag = responseXML.getElementsByTagName("existuserid")[0];
  var existUserID = existUserID_tag.childNodes[0].nodeValue;
  //alert(existUserID);
  if (existUserID=="1") {
    if ( confirm("「該用戸ID」己存在、是否進行修改?") ) {
      document.myForm.actionbtn.value = "Update";
      document.myForm.submit();
    }
  } else {
    document.myForm.actionbtn.value = "Insert";
    document.myForm.submit();
  }
}

function doAdd() {
  var url = "testServlet?actionbtn=Check";
  var ajax = new AJAXInteraction(url);
  ajax.send();
}

//--></SCRIPT>
</head>
 
<body>
  <form name="myForm" action="testServlet" method="post">
    <input type="text" name="userid" value="">
    <input type="hidden" name="actionbtn" value="">
    <input type="button" value="登録" onclick="doAdd()">
  </form>
</body>
</html>
 
------------- testServlet.java ---------------------------------------------------------------
//HTTP Post処理
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String actionbtn = request.getParameter("actionbtn");
  String userid = request.getParameter("userid");

  if (actionbtn.equals("Check")) {
  //----------- 験証UserID、用Ajax返回結果 ----------------------
    String check_userid="0";
    if (dataModule.exist_userid(userid)) { //験証処理(略)
      check_userid = "1"; //若存在
    }
    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");
    response.getWriter().write("<existuserid>" + check_userid + "</existuserid>");
  } else {
  //----------- 用forward頁移動 ---------------------------------
   String result_message = "有問題";
   if (actionbtn.equals("Insert")) { 
      if (dataModule.add_user(userid))) { //挿入処理(略)
        result_message = "挿入成功";
      }
    }
    else if (actionbtn.equals("Update")) { 
      if (dataModule.edit_user(userid)) { //修改処理(略)
        result_message = "修改成功";
      }
    }
    request.setAttribute("result_message", result_message);
    RequestDispatcher d = request.getRequestDispatcher("/other.jsp");
    d.forward(request, response);
  }
}

//HTTP Get処理
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5561次
    • 积分:122
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论