某马旅游网的注册功能
注册界面
功能分析
注册界面验证代码跟ajax
<script>
function checkUsername() {
let username = $("#username").val();
// 注意量词是怎么写的
let reg_username = /^\w{8,20}$/;
let flag =reg_username.test(username);
if (flag){
$("#username").css("border","");
} else {
$("#username").css("border","1px solid red");
}
return flag;
}
function checkPassword() {
let password = $("#password").val();
// 注意量词是怎么写的
let reg_password = /^\w{8,20}$/;
let flag =reg_password.test(password);
if (flag){
$("#password").css("border","");
} else {
$("#password").css("border","1px solid red");
}
return flag;
}
function checkEmail() {
let email = $("#email").val();
// 注意量词是怎么写的
let reg_email = /^(w{3})?.\w+@\w+.\w+$/;
let 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()) {
$.post("registUserServlet",$(this).serialize(),function (data) {
if (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>
UserDaoImpl
package cn.itcast.travel.dao.impl;
import cn.itcast.travel.dao.UserDao;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDaoImpl implements UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public User findByUsername(String username) {
User user = null;
try {
String sql = "select * from tab_user where username = ?";
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,status,code) values(?,?,?,?,?,?,?,?,?)";
// 2. 执行sql
template.update(sql,user.getUsername(),
user.getPassword(),
user.getName(),
user.getBirthday(),
user.getSex(),
user.getTelephone(),
user.getEmail(),
user.getStatus(),
user.getCode());
}
}
UserServiceImpl
package cn.itcast.travel.service.impl;
import cn.itcast.travel.dao.UserDao;
import cn.itcast.travel.dao.impl.UserDaoImpl;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl();
@Override
public boolean regist(User user) {
User u = userDao.findByUsername(user.getUsername());
if (u != null){
return false;
}
userDao.save(user);
return true;
}
}
RegistUserServlet
package cn.itcast.travel.web.servlet;
import cn.itcast.travel.domain.ResultInfo;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.UserService;
import cn.itcast.travel.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@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("验证码错误!");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(info);
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(json);
return;
}
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();
}
UserService service = new UserServiceImpl();
boolean flag = service.regist(user);
ResultInfo info = new ResultInfo();
if (flag){
// 注册成功
info.setFlag(true);
} else {
// 注册失败
info.setFlag(false);
info.setErrorMsg("注册失败!");
}
response.setContentType("application/json;charset=utf-8");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(info);
// 将json数据写回客户端
response.getWriter().write(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
因为我们应该先验证验证码所以当验证码不正确或者验证码为空时,就直接返回。然而我们注册的逻辑是:判断表单的格式是否正确(正则表达式),验证码是否正确, 是否存在该用户名(查寻数据库)