《黑马旅游网》综合案例(三)注册功能后台
Servlet代码实现
UserDao:
public interface UserDao { /** * 根据用户名查询用户信息 * @param username * @return */ public User findByUsername(String username); /** * 用户保存 * @param user */ public void save(User user); }
UserDaoImpl:
public class UserDaoImpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public User findByUsername(String username) { User user = null; try { //定义sql String sql = "select * from tab_user where username = ?"; //执行sql user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username); } catch (DataAccessException e) { } return user; } @Override public void save(User user) { //定义sql String sql = "insert into tab_user(username,password,name,birthday,sex,telephone,email) values (?,?,?,?,?,?,?)"; //执行sql template.update(sql, user.getUsername(), user.getPassword(), user.getName(), user.getBirthday(), user.getSex(), user.getTelephone(), user.getEmail()); } }
UserService:
public interface UserService { /** * 注册用户 * @param user * @return */ boolean regist(User user); }
UserServiceImpl:
public class UserServiceImpl implements UserService { private UserDao userDao = new UserDaoImpl(); /** * 注册用户 * @param user * @return */ @Override public boolean regist(User user) { //根据用户名查询用户对象 User u = userDao.findByUsername(user.getUsername()); //判断是否为null if (u!=null){ //用户名存在,注册失败 return false; } //保存用户信息 userDao.save(user); return true; } }
RegistUserServlet:
@WebServlet("/registUserServlet") public class RegistUserServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取数据 Map<String, String[]> map = request.getParameterMap(); //封装对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //调用service完成注册 UserServiceImpl service = new UserServiceImpl(); boolean flag = service.regist(user); ResultInfo info = new ResultInfo(); //响应结果 if (flag){ //注册成功 info.setFlag(true); }else { //注册失败 info.setFlag(false); info.setErrorMsg("注册失败!"); } //将info对象序列化json并且写回客户端 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
访问注册页面添加数据
查看数据库添加成功
验证码和前台处理:
在RegistUserServlet中前面加入验证码的校验:
先进行校验在获取其他数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证码的校验 String check = request.getParameter("check"); //从session中获取验证码 HttpSession session = request.getSession(); String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER");//保证验证码只使用一次 //比较 if (checkcode_server==null || !checkcode_server.equalsIgnoreCase(check)){ //验证码错误 ResultInfo info = new ResultInfo(); //注册失败 info.setFlag(false); info.setErrorMsg("验证码错误!"); //将info对象序列化json并且写回客户端 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; } //获取数据 .....