Ajax验证用户名是否被注册
var xmlHttp;
???function createXMLHttpRequest(){ ??????????????????????????????????????????????????????// 创建XMLHttp请求对象
???????if(window.ActiveXObject){
???????????xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
???????}else if(window.XMLHttpRequest){
???????????xmlHttp=new XMLHttpRequest();
???????}
???}
xmlHttp.onreadystatechange=处理方法
xmlHttp:这是XMLHttpRequest对象名称
处理方法:这是自定义的JavaScript方法名称
???function startRequest(userName){ ???????????????????????????????????????????????????????// 发送页面请求
???????createXMLHttpRequest();
???????xmlHttp.onreadystatechange=handleStateChange; ?????????????????????// 指定处理服务器响应的方法
???????xmlHttp.open("GET","regsiterServlet?userName="+userName,true);
???????xmlHttp.send(null);
???}
<script type="text/javascript">
???var xmlHttp;
???function createXMLHttpRequest(){ ??????????????????????????????????????????????????????// 创建XMLHttp请求对象
???????if(window.ActiveXObject){
???????????xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
???????}else if(window.XMLHttpRequest){
???????????xmlHttp=new XMLHttpRequest();
???????}
???}
???function handleStateChange(){ ???????????????????????????????????????????????// 处理应答数据
???????if(xmlHttp.readyState==4){
???????????if(xmlHttp.status==200){
???????????????document.getElementById(‘text‘).innerHTML=xmlHttp.responseText;
???????????}
???????}
???}
???function startRequest(userName){ ???????????????????????????????????????????????????????// 发送页面请求
???????createXMLHttpRequest();
???????xmlHttp.onreadystatechange=handleStateChange;
???????xmlHttp.open("GET","regsiterServlet?userName="+userName,true);
???????xmlHttp.send(null);
???}
</script>
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
???????throws ServletException, IOException {
???response.setContentType("text/html;charset=UTF-8");
???PrintWriter out = response.getWriter(); ???????????????????????????????// 获取应答对象的字符输出流
???try {
???????UserDao dao = new UserDao(); ??????????????????????// 创建数据库操作类
???????String userName = request.getParameter("userName"); ?????// 获取请求对象的用户名参数
???????if(userName==null||userName.isEmpty()){
???????????// 返回提示信息
???????????out.print("<font color=‘red‘>必须输入用户名</font>");
???????????return;
???????}
???????boolean hasUser = dao.hasUser(userName); ???????????// 判断数据库是否存在指定用户名
???????if (hasUser) { ????????????????????????????????????????????// 如果存在重名情况
???????????// 返回已注册信息
???????????out.print("<font color=‘red‘>该用户名已经被注册</font>");
???????} else { ?????????????????????????????????????????// 否则
???????????// 提示用户可以注册
???????????out.print("<font color=‘green‘>该用户名可以使用</font>");
???????}
???} catch (Exception ex) {
???????Logger.getLogger(RegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
???} finally {
???????out.close();
???}
}
public boolean hasUser(String userName) {
???boolean hasUser = false; ???????????????// 声明检查变量,false代表没有重名用户
???Connection conn = getConn(); ?????????????????// 连接数据库
???if (conn != null) {
???????// 定义SQL查询语句
???????String sql = "SELECT count(*) FROM tb_checkUser where username=?";
???????try {
???????????PreparedStatement ps = conn.prepareStatement(sql);
???????????ps.setString(1, userName); ??????// 设置查询语句的参数
???????????ResultSet rs = ps.executeQuery(); ?????// 执行SQL查询
???????????if (rs.next()) {
???????????????int count = rs.getInt(1); ????????????// 获取同名用户数量
???????????????if (count > 0) { ?????????????????// 如果同名用户数量大于0
???????????????????hasUser = true; ?????????????// 设置检查变量为true,代表用户重名
???????????????}
???????????}
???????????rs.close(); ?????????????????????????// 是否资源
???????????ps.close();
???????????conn.close();
???????} catch (SQLException ex) {
???????????Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
???????}
???}
???return hasUser;
}
Ajax验证用户名是否被注册
原文地址:https://www.cnblogs.com/hao-lei/p/8360890.html