JavaWeb【旅游项目】注册功能
标题注册功能的后台代码
TestUserService
测试service的register方法
@Test
public void test03() {
UserService userService = new UserService();
User user = new User();
user.setUsername("jackhello");
user.setPassword("123456");
user.setStatus('Y');
int code = userService.register(user);
System.out.println(code);
}
UserService
public int register(User user) {
UserDao userDao = MySessionUtils2.getMapper(UserDao.class);
//判断 用户的账号是否存在
User user2 = userDao.findByUserName(user.getUsername());
//不存在,调用保存 返回 1
if(user2 == null){
userDao.save(user2);
return 1;
}else{
//存在,返回-1
return -1;
}
}
UserDao
public interface UserDao {
User findByUserName(String username);
void save(User user2);
}
UserDao.xml
<select id="findByUserName" parameterType="string" resultType="user">
select * from tab_user where username=#{username}
</select>
<insert id="save" parameterType="user" >
insert into tab_user values(null,#{username},
#{password},#{name},#{birthday},#{sex},#{telephone},#{email},#{status},#{code})
</insert>
UserDaoTest2
测试UserDao类的方法
@Test
public void findByUserName() {
UserDao dao = MySessionUtils2.getMapper(UserDao.class);
User user = dao.findByUserName("jackhello");
System.out.println(user);
}
@Test
public void save() {
UserDao dao = MySessionUtils2.getMapper(UserDao.class);
User user= new User(33,"jackhello33","jackhello34",new Date(),"jack","男","3333","333@qq.com","N","ddd");
// User(int uid, String username, String password, Date birthday, String name, String sex, String telephone, String email, char status, String code)
dao.save(user);
MySessionUtils2.commitAndClose();
}
前台代码
rejister.jsp
<script type="text/javascript" >
function checkUsername(){
//判断一下账号输入是否合法
var username = $("#username").val();
var reg = /^\w{8,20}$/ ;
var flag = reg.test(username);
if(flag){
//不提示
$("#username").css("border","");
}else{
//提示
$("#username").css("border","1px solid red");
}
return flag;
}
function checkPassword(){
//判断一下密码输入是否合法
var password = $("#password").val();
var reg = /^\w{8,20}$/ ;
var flag = reg.test(password);
if(flag){
//不提示
$("#password").css("border","");
}else{
//提示
$("#password").css("border","1px solid red");
}
return flag;
}
function checkEmail(){
//判断一下密码输入是否合法
var email = $("#email").val();
var reg = /^\w+@\w+\.\w+$/ ;
var flag = reg.test(email);
if(flag){
//不提示
$("#email").css("border","");
}else{
//提示
$("#email").css("border","1px solid red");
}
return flag;
}
$(function () {
//账号密码 手机号 邮箱 正则判断
//格式正确不作提示,如果错误将边框改成红色
$("#username").blur(checkUsername)
$("#password").blur(checkPassword)
$("#email").blur(checkEmail)
$("#registerForm").submit(function () {
//ajax提交
if(checkUsername()&&checkPassword()&&checkEmail()){
//使用jquery
$.post("registerServlet",$("#registerForm"). serialize(),
function(data){
//{code:1,data:成功}
if(1 == data.code){
//注册成功
window.location = "login.jsp";
}else{
//注册失败
$("#errorMsg").html(data.data);
}
},"json");
}
return false;
})
});
</script>
RegisterServlet
@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
static {
//静态代码块,在加载的时候自动执行,而且只执行一次
//将string转date的转换器注册给beanUtils
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}, Date.class);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从请求中获取check1码
String check1 = request.getParameter("check");
//从session中获取check2码
String check2 = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
System.out.println(check1);
System.out.println(check2);
//从session中删除check2码
request.getSession().removeAttribute("CHECKCODE_SERVER");
//比较check1 与 check2
//相同表示验证码不正确,将提示信息写到页面的错误提示
if(check1 == null || !check1.equalsIgnoreCase(check2)){
//验证码不看大小写
ResponseInfo responseInfo = new ResponseInfo();
responseInfo.setCode(-4);
responseInfo.setData("注册失败,验证码出错");
//json
String json = new ObjectMapper().writeValueAsString(responseInfo);
response.getWriter().println(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();
}
System.out.println("注册数据"+user);
//对参数处理
UserService userService = new UserService();
int code = userService.register(user);
//响应给浏览器
ResponseInfo responseInfo = new ResponseInfo();
responseInfo.setCode(code);
if(code==1){
responseInfo.setData("注册成功");
}else{//-1,-2,-3
responseInfo.setData("注册失败");
}
String json = new ObjectMapper().writeValueAsString(responseInfo);
response.getWriter().println(json);
}
}