Maven项目JavaWeb网站开发之用户注册功能五分钟快速实现构思简单操作基础

上期我们讲了登录功能的实现,本期开始讲注册功能的实现。思路如下:

1、在前端写一个register.html(四个元素:一个昵称框,一个账号框,一个密码框,一个注册按钮),写一个ajax往后端传数据

2、在dao层的UserDAO接口封装两个方法(一个注册功能方法,一个用户名重复不能注册方法)

3、在dao-->Impl层的UserDAOImpl实现两个方法(两个sql语句)

4、在Servlet层写一个register.java实现注册功能的url

 步骤一:前端

<label for="usernick">昵称</label><input type="text" name="usernick" id="usernick"><br>
<label for="name">账号</label><input type="text" name="name" id="name"><br>
<label for="password">密码</label><input type="password" name="password" id="password"><br>
<button type="button" onclick="register()">注册</button>

<script src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
    function register(){
        let usernick = $('#usernick').val();
        let name = $('#name').val();
        let password = $('#password').val();
        console.log(usernick,name,password);

        $.ajax({
            type:'post',
            url:'/register',
            data:{"usernick":usernick,"name":name,"password":password},
            dataType:'text',
            success:function (resp){
                if (resp === 'success'){
                    window.alert("注册成功,即将跳转到登录界面!");
                    setTimeout(function (){
                        window.location.href="../index.html";
                    },1500);
                }else {
                    window.alert("用户名重复");
                }
            }
        })
    }
</script>

步骤二:UserDAO层封装两个方法

//    用户注册--添加用户
    int register(User user) throws SQLException;
//    用户注册--查找所有账号,重复则不能注册
    User selectUserName(String name) throws SQLException;

 步骤三:UserDAOImpl实现两个方法

    @Override
    public int register(User user) throws SQLException {
        DBUtils db = new DBUtils();
        String sql = "insert into user(usernick,name,password) value(?,?,?)";
        PreparedStatement ps = db.getPreparedStatement(sql);
        ps.setString(1, user.getUsernick());
        ps.setString(2, user.getName());
        ps.setString(3, user.getPassword());
        int rs = ps.executeUpdate();
        if (rs == 1){
            db.commit();
            return 1;
        }else {
            db.connectionRollback();
            return 0;
        }
    }

    @Override
    public User selectUserName(String name) throws SQLException {
        DBUtils db = new DBUtils();
        String sql = "select * from user where name=?";
        PreparedStatement ps = db.getPreparedStatement(sql);
        ps.setString(1,name);
        System.out.println(ps);
        ResultSet rs = ps.executeQuery();
        User user = new User();
        while(rs.next()){
            user.setName(rs.getString("name"));
            return user;
        }
        return null;
    }

 步骤四:Servlet层写register.java

@WebServlet("/register")
public class register extends HttpServlet {
    private final UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String usernick = req.getParameter("usernick");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        try {
            User user = userService.selectUserName(name);
            System.out.println(user);
            if (user != null){
                resp.getWriter().write("fail");
            }else {
                user = new User();
                user.setUsernick(usernick);
                user.setName(name);
                user.setPassword(password);
                int s = userService.register(user);
                if (s == 1){
                    resp.getWriter().write("success");
                }else {
                    resp.getWriter().write("fail");
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

以上就实现了用户注册功能,细心的小伙伴记得把UserDAO层方法封装到UserService层哦,并在UserServiceImpl指向该方法,具体方法如下:

 隐藏步骤五:将dao层UserDAO接口方法写到service层UserService接口下,并在service-->Impl层的UserServiceImpl指向dao-->Impl层UserDAOImpl实现安全封装

 UserService接口:

public interface UserService {
    //    用户登录
    User userLogin(String name,String password) throws SQLException;
    //    用户注册--添加用户
    int register(User user) throws SQLException;
    //    用户注册--查找所有账号,重复则不能注册
    User selectUserName(String name) throws SQLException;
}

 UserServiceImpl.java:

public class UserServiceImpl implements UserService {
    UserDAO userDAO = new UserDAOImpl();
    @Override
    public User userLogin(String name, String password) throws SQLException {
        return userDAO.userLogin(name,password);
    }
    @Override
    public int register(User user) throws SQLException {
        return userDAO.register(user);
    }
    @Override
    public User selectUserName(String name) throws SQLException {
        return userDAO.selectUserName(name);
    }
}

 总体结构如下:

本期分享结束,谢谢大家观看。

本期语录:总要留给自己一点放空的时间,勇敢的做自己想做的事情去吧。