最近在用jsp写注册,现在想通过使用Ajax来判断注册用户是否存在,请各位大侠分享一下代码
第1个回答 推荐于2016-08-07
<html>
<head>
<script type="text/javascript">
function $(tagId){
return document.getElementById(tagId);
}
function checkUserName(){
var name = $("nameId").value;
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
xhr.open("POST","/Ajax/userNameCheckServlet");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("userNeme="+name);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var ret = xhr.responseText;
var span = $("msgId");
//innerHTML作用 方便往标签中增加内容
span.innerHTML = "<font color='red'>"+ret+"</font>" ;
}
}
}
</script>
</head>
<body>
<table>
<tr>
<td>用户名:<input type="text" id="nameId" onblur="checkUserName();"/>
<span id="msgId"></span></td>
</tr>
<tr>
<td>密 码:<input type="password"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="确定"/></td>
</tr>
</table>
</body>
</html>
然后在servlet里判断是否用户名存在:
package com.wsw.day01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserNameCheckServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
String userName = request.getParameter("userNeme");
System.out.println(userName);
PrintWriter out = response.getWriter();
if("wsw".equals(userName)){
out.print("is ok");
}else{
out.print("is false");
}
out.flush();
}
}
这里是简单的判断,实际应用的时候要连数据库,查询判断,反正都是这个原理本回答被网友采纳
<head>
<script type="text/javascript">
function $(tagId){
return document.getElementById(tagId);
}
function checkUserName(){
var name = $("nameId").value;
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
xhr.open("POST","/Ajax/userNameCheckServlet");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("userNeme="+name);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var ret = xhr.responseText;
var span = $("msgId");
//innerHTML作用 方便往标签中增加内容
span.innerHTML = "<font color='red'>"+ret+"</font>" ;
}
}
}
</script>
</head>
<body>
<table>
<tr>
<td>用户名:<input type="text" id="nameId" onblur="checkUserName();"/>
<span id="msgId"></span></td>
</tr>
<tr>
<td>密 码:<input type="password"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="确定"/></td>
</tr>
</table>
</body>
</html>
然后在servlet里判断是否用户名存在:
package com.wsw.day01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserNameCheckServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
String userName = request.getParameter("userNeme");
System.out.println(userName);
PrintWriter out = response.getWriter();
if("wsw".equals(userName)){
out.print("is ok");
}else{
out.print("is false");
}
out.flush();
}
}
这里是简单的判断,实际应用的时候要连数据库,查询判断,反正都是这个原理本回答被网友采纳