验证码和前台数据处理结果
RegistUserServlet类:
@WebServlet("/registUserServlet") public class RegistUserServlet extends HttpServlet { 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); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; } //1、获取数据 Map<String, String[]> map = request.getParameterMap(); //2、封装对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //3、调用service完成注册 UserService service = new UserServiceImpl(); boolean flag = service.regist(user); //4、响应结果 ResultInfo info = new ResultInfo(); if (flag){ //注册成功 info.setFlag(true); }else { //注册失败 info.setFlag(false); info.setErrorMsg("注册失败"); } //将info对象序列化为json ObjectMapper mapper = new ObjectMapper(); String string = mapper.writeValueAsString(info); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(string); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
register.html页面:
<script> /* 表单校验: 1、用户名:单词字符,长度8到20位 2、密码:单词字符,长度8到20位 3、email:邮件格式 4、姓名:非空 5、手机号:手机号格式 6、出生日期:非空 7、验证码:非空 */ //校验用户名 function checkUsername() { //1、获取用户名 var usernmae = $("#username").val(); //2、定义正则 var reg_username = /^\w{8,20}$/; //判断给出提示信息 var flag = reg_username.test(usernmae); if (flag){ //用户名合法 $("#username").css("border",""); } else { //用户名非法 $("#username").css("border","1px solid red"); } return flag; } //校验密码 function checkPassword(){ //1、获取用户名 var password = $("#password").val(); //2、定义正则 var reg_password = /^\w{8,20}$/; //判断给出提示信息 var flag = reg_password.test(password); if (flag){ //用户名合法 $("#password").css("border",""); } else { //用户名非法 $("#password").css("border","1px solid red"); } return flag; } //校验邮箱 function checkEmail(){ //1、获取邮箱 var email = $("#email").val(); //定义规则 var reg_email = /^\w+@\w+\.\w+$/; //判断 var flag = reg_email.test(email); if (flag){ $("#email").css("border",""); }else { $("#email").css("border","1px solid red"); } return flag; } $(function () { $("#registerForm").submit(function () { //发送数据到服务器 if (checkUsername() && checkPassword() && checkEmail()){ //校验通过发送ajax请求,提交表单的数据 $.post("registUserServlet",$(this).serialize(),function (data) { //处理服务器响应的数据 if(data.flag){ //注册成功,跳转成功页面 location.href = "register_ok.html"; }else { $("#errorMsg").html(data.errorMsg); } }); } return false; }); $("#username").blur(checkUsername); $("#password").blur(checkPassword); $("#email").blur(checkEmail); }) </script>
标签:info,用户名,验证码,var,flag,前台,数据处理,password,email From: https://www.cnblogs.com/xjw12345/p/16618619.html