1.将表单提交至servlet
//请求这个servlet
<form action="<c:url value='/UserServlet'/>" method="post" id="registForm">
//请求servlet某个方法,接下来要准备userServlet类及regist方法
<input type="hidden" name="method" value="regist"/>
...
public class UserServlet extends BaseServlet{
private UserService userService = new UserService();
private String ajaxValidateEmail(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
//1.获取email
String email =req.ajaxValidateEmail(email);
//2.通过service得到校验结果
boolean b = userService.ajaxValidateEmail(email);
//3.发送给客户端
resp.getWriter().print(b);
return null;
}
//注册功能
public String regist(HttpServletRequest req, HttpServletResponse resp) throw Exception{
//1.封装表单数据到User对象中
User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
//2.校验,如果失败,保存信息,返回register.jsp
req.setAttribute("form",formUser);
req.setAttribute("errors",errors);
return "f:/jsps/user/regist.jsp";
//3.使用service完成业务
userService.regist(formUser);
//4.保存成功信息,转发到msg.jsp显示
req.setAttribute("code","success");
req.setAttribute("msg","注册成功,情马上到邮箱激活");
return "f:/jsps/msg.jsp";
}
//后台注册校验
private Map<String,String> validateRegist(User formUser){
Map<String,String> errors = new HashMap();
String loginname = formUser.getLoginname();
if(loginname == null || loginname.trim.isEmpty()){
errors.put("username","用户名不能为空");
}
}
}
2.表单提交时校验
$(#registForm).submit(function(){
validateLoginname();
return false;//拦截
});
3.后台校验
//焦点消失时发送异步请求
UserServlet
提交页面ajax校验方法
1.校验用户名是否注册
2.校验email是否注册
3.校验码是否正确//验证码保存在session中
访问数据库还得写service层,提供如下方法
UserService
1.校验用户名
2.校验Email
UserDao
1.校验用户名
2.校验Email
public class User{
//对象数据库表
private String uid;//主键
private String loginname;//登录名
...
//对应注册表单
private String reloginpass;//确认密码
private String verifyCode;//验证码
//修改密码表单
private String newloginpass;//新密码
}
public class UserDao{
private QueryRunner qr = new TxQueryRunner();
public boolean ajaxValidateLoginname(String loginname){
String sql = "select count(1) from t_user where loginname=?";
Number number = (Number)qr.query(sql,new SaclarHandler(),loginname);
return number.intValue() == 0;
}
}
public class UserService{
private UserDao userDao = new UserDao();
//用户名注册校验
public boolean ajaxValidateLoginname(String loginname){
return userDao.ajaxValidateLoginname(loginname);
}
}
4.ajax学习
$.ajax(
{
url:Servlet地址,
data:{method:"ajaxValidateLoginname",loginname:value}//参数,
type:POST,
dataType:json(表示服务器返回的数据类型经),
async:false,//是否异步请求,如果是异步那么不会等服务器返回,函数就往下走
chche:false,
success:function(result){
if(!result){
$(#+id+"Error").text("用户名已被调用");
showError($("#"+id+"Error"));
return false;
}
}
}
);
5.UserServlet#regist(){//相当于秘书的活
- 1.封装表单数据到javaBean中
- 2.校验参数(前端校验不可靠,后端还要校验)
- 3.把表单数据给service完成业务逻辑//相当于老板的活
- 4.保存成功信息
- 5.转发到写字板显示成功信息 }
UserService#regist(User){
- 1.补齐其他数据(id,状态,激活码)
- 2.userDao把数据保存到数据库中
- 3.发邮件 }
UserDao#add(User){ 略 }
实际代码:
Dao层:
public void add(User user)throw SQLException{
String sql="insert into t_user values(?,?,?,?,?,?)";
Object[] params = {user.getUid,user.getLoginname,...};
qr.update(sql,params);
}
UserService层
public void regist(User user){
//1.数据的补齐
user.setUid(CommonUtils.uuid);
user.setStatus(false);
user.setActivationCode(CommonUtils.uuid+CommonUtils.uuid);
//2.向数据库中插入
userDao.add(user);
//3.发邮件
//把配置文件内容加载到prop中
Properties prop = new Properties();
prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
- 1.登陆邮件服务器,得到session
String host = prop.getProperty("host");//"服务器主机名"
String name = prop.getProperty("username");//登录名
String pass = "";//登陆密码
Session session = MailUtils.createSession(host,name,pas
- 2.创建Mail对象
String from = "";
String to = "";
String subject = "";
String content =
//format把{0}用第二个参数替换
MessageFormat.format(prop.getProperty("content"),user.getActivationCode());
Mail mail = new Mail(from,to,subject,content);
- 3.发送邮件
MailUtils.send(session,mail);
}