注册时判断用户名是否存在

注册时对用户名的判断

内含JDBC的封装,详细资料请看JDBC工具类的封装

1、user_Add.html(注册页面)


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册页面title>
    <script type="text/javascript">
        function fun1() {
      
            //1、创建一个异步请求对象
            var xmlHttp = new XMLHttpRequest();
            //2、为异步请求对象身上绑定【工作状态监听器】
            xmlHttp.onreadystatechange = function () {
      
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
      
                    var data = xmlHttp.responseText;
                    callBack(data);
                }
            };
            //3、初始化异步请求对象
            var param = document.getElementById("username").value;
            xmlHttp.open("get","/myWeb/user/check?username=" + param,true);
            //4、通知异步请求对象代替浏览器发送请求协议包
            xmlHttp.send();
        }
        //局部刷新函数
        function callBack(param) {
      
            if(param == 0){
      
                document.getElementById("myFont").innerText='用户名可以使用';
            } else {
      
                document.getElementById("myFont").innerText='亲,用户名已被占用';
            }
        }
    script>
head>
<body>
        <form action="/myWeb/user/add" method="get">
            用户姓名:<input id="username" type="text" name="username" onblur="fun1()">
                    <font color="red" id="myFont">font><br>
            用户密码:<input type="password" name="password"><br>
            用户性别:<input type="radio" name="sex" value=""><input type="radio" name="sex" value=""><br>
            用户邮箱:<input type="text" name="email"><br>
                    <input type="submit" value="用户注册">
                    <input type="reset" value="重置">
        form>
body>
html>

2、UserCheckServlet(逻辑处理)

package com.university.controller;

import com.university.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserCheckServlet extends HttpServlet {
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        UserDao dao = new UserDao();
        int result = 0;
        //1.读取请求协议包参数[部门名称]
        String username = request.getParameter("username");
        //2.Dao层查询这个用户名称是否已经存在
        result = dao.checkName(username);
        //3.将查询结果写入到[响应包]
        response.getWriter().println(result);
    }//Tomcat将[响应包]推送给当前的异步请求对象
}

3、web.xml(Servlet配置)

<servlet>
    <servlet-name>UserCheckServletservlet-name>
    <servlet-class>com.university.controller.UserCheckServletservlet-class>
servlet>
<servlet-mapping>
    <servlet-name>UserCheckServletservlet-name>
    <url-pattern>/user/checkurl-pattern>
servlet-mapping>

4、UserDao(数据库处理)

package com.university.dao;

import com.university.entity.Users;
import com.university.util.JdbcUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
     
    private JdbcUtil util = new JdbcUtil();
    //注册查看用户名是否存在
    public int checkName(String username){
     
        String sql = "select * from users where username=?";
        int result = 0;
        ResultSet rs = null;
        List userList = new ArrayList();
        try {
     
            PreparedStatement ps = util.createStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            while (rs.next()){
     
                String username1 = rs.getString("username");
                Users users = new Users(null,username1,null,null,null);
                userList.add(users);
                result = userList.size();
            }
        } catch (SQLException e) {
     
            e.printStackTrace();
        } finally {
     
            util.close(rs);
        }
        return result;
    }
}

5、测试

(1)输入已有用户名(当文本框失去焦点时进行判断)

注册时判断用户名是否存在_第1张图片

(2)输入新用户名(当文本框失去焦点时进行判断)

注册时判断用户名是否存在_第2张图片

你可能感兴趣的:(业务实现,servlet,ajax,数据库)