J2EE实现用户注册
效果图
注册页面(register.html)
<!-- main -->
<div class="main" >
<h1>
云笔记登录系统
</h1>
<div>
<input type="text" value="用户名" id="username" name="username" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '用户名';}" required=""><br><br>
<input type="text" value="密码" id="password" name="password" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" required=""><br><br>
<input type="text" value="确认密码" id="repassword" name="repassword" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" required=""><br><br>
<input type="text" value="姓名" id="name" name="name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" required=""><br><br>
<input type="text" value="年龄" id="age" name="age" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '用户名';}" required=""><br><br>
<input type="button" class="submit" id="submit" value="注册">
<input type="reset" id="reset" value="重置">
</div>
<!-- <form> -->
<!-- </form> -->
</div>
<!-- //main -->
register.js
$(function(){
$('#reset').click(function(){
$('#username').val('');
$('#password').val('');
$('#repassword').val('');
$('#name').val('');
$('#age').val('');
})
$('#submit').click(function(){
/*不能为空判断*/
if($('#username').val()=='')
{
alert('用户名不能为空');
$('#username').focus();
return ;
}else if($('#password').val()=='')
{
alert('密码不能为空');
$('#password').focus();
return ;
}else if($('#repassword').val()=='')
{
alert('确认密码不能为空');
$('#repassword').focus();
return ;
}else if($('#name').val()=='')
{
alert('姓名不能为空');
$('#name').focus();
return ;
}else if($('#age').val()=='')
{
alert('年龄不能为空');
$('#age').focus();
return ;
}
/*判断密码和确认密码输入是否一致*/
if($('#password').val()!=$('#repassword').val()){
alert('密码和确认密码不一致')
$('#repassword').focus();
return;
}
/*判断年龄输入是否合法*/
var reg=/^(?:[1-9][0-9]?|1[01][0-9]|120)$/;
if(reg.test($('#age').val())){
}else{
alert('年龄输入不合法,1-120');
$('#age').focus();
return;
}
var sendData={"username":$('#username').val(),"password":$('#password').val(),"name":$('#name').val(),"age":$('#age').val()};
$.ajax({
type:"post",
url:"user/register.do",
data:sendData,
datatype:"json",
success:function(msg){
if(msg.state==1){
//说明注册成功
alert(msg.message);
}else{
//注册失败
alert(msg.message);
}
},
error:function(msg){
alert("通信失败");
}
});
})
});
创建UserColler.java
@RequestMapping("/register")
@ResponseBody
public Object register(User user) {
user.setId(UUID.randomUUID().toString());
try {
String newpassword = MD5.md5(user.getPassword(), "helloworld");
user.setPassword(newpassword);
} catch (Exception e) {
e.printStackTrace();
}
JsonResult jsonResult = null;
System.out.println(user);
// 1.判断前台传过来的用户名是否重复
User userResult = userService.getUserByUserName(user);
if (userResult == null) {
// 可以注册
// 2.如果不重复,说明可以注册,把User信息添加到数据库
int result = userService.addUser(user);
if (result == 1) {
// 数据库插入成功
jsonResult = new JsonResult(JsonResult.STATE_SUCCESS, "注册成功", null);
} else {
// 数据库插入失败
jsonResult = new JsonResult(JsonResult.STATE_ERROR, "注册失败", null);
}
} else {
// 存在相同的用户名,不可以注册
jsonResult = new JsonResult(JsonResult.STATE_ERROR, "存在重复的用户名", null);
}
return jsonResult;
}
创建service层(UserService.java)
package com.whc.noteserver.service;
import com.whc.noteserver.entity.User;
public interface UserService {
public int addUser(User user);
}
创建Dao
package com.whc.noteserver.dao;
import com.whc.noteserver.entity.User;
public interface UserDao {
/**
* 添加用户信息
*
* @param user 从前台传入的user
* @return 1
*/
public int addUser(User user);
}