JAVA项目之注册

public class RegisterServlet extends HttpServlet {
    private UsersService usersService = new UsersService();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决request的中文乱码
        request.setCharacterEncoding("UTF-8");
        // 获取用户输入的验证码
        String checkcode = request.getParameter("checkCode");
        // 获取Session中的验证码
        String code = (String) request.getSession().getAttribute("checkcode_session");
        // 进行比较判断
        if (!checkcode.equals(code)) {        
            //System.out.println("验证码错误!");
            request.setAttribute("info", "验证码错误!");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            return;
        }

        // 获取所有请求参数的MAP集合
        Map<String, String[]> map = request.getParameterMap();
        // 创建users对象
        Users users = new Users();
        // 用BeanUtils中的方法将map集合中的参数封装到Users对象中 // 原理:根据map中的key 跟user对象中的属性名一一映射
        try {
            BeanUtils.populate(users, map);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 封装uid(随机产生36位不重复的id)
        users.setUid(UUID.randomUUID().toString());
        // 调Service方法
        System.out.println(users);
        usersService.register(users);
        // 成功后,重定向到登录页面
        response.sendRedirect(request.getContextPath() + "/login.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
private UsersDao usersDao=new UsersDao();
    //注册
    public void register(Users users){
        try {
            usersDao.register(users);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
// 注册
    public void register(Users users) throws SQLException {
        /*
         * Connection conn = JDBCUtils.getConn(); String sql =
         * "insert into users(uid,username,password,email,name,sex,birthday) values(?,?,?,?,?,?,?)"
         * ; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1,
         * users.getUid()); pst.setString(2, users.getUsername());
         * pst.setString(3, users.getPassword()); pst.setString(4,
         * users.getEmail()); pst.setString(5, users.getName());
         * pst.setString(6, users.getSex()); pst.setString(7,
         * users.getBirthday()); //执行sql pst.executeUpdate(); //释放资源
         * JDBCUtils.close(conn, pst);
         */
        QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource());
        String sql = "insert into users(uid,username,password,email,name,sex,birthday) values(?,?,?,?,?,?,?)";
        qr.update(sql, new Object[] { users.getUid(), users.getUsername(), users.getPassword(), users.getEmail(),
                users.getName(), users.getSex(), users.getBirthday() });

    }

 

点赞