开发者学堂课程【Java Web项目实战2:图书商城:用户模块之注册功能完成】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/35/detail/767
用户模块之注册功能完成
UserDao.java:
1.
package cn.itcast.bookstore.user.dao;
2.
import java.sql.SQLException;
3.
/**
4.
*User持久层
5.
* @author cxf
6.
*
7.
*/
8.
public class UserDao {
9.
private QueryRunner qr = new TxQueryRunner();
10.
/**
11.
按用户名查询
12.
*@param username
13.
*@return
14.
*/
15.
public User findByUsername(String username){
16.
try {
17.
String sql = "select * from tb_user where username=?";
18.
return qr.query(sql,new BeanHandler<User>(User.class),username);
19.
} catch (SQLException e) (
20.
throw new RuntimeException(e);
21.
}
22.
}
23.
/**
24.
按用户名查询
25.
*@param username
26.
*@return
27.
*/
28.
public User findByUsername(String
email
){
29.
try {
30.
String sql = "select * from tb_user where
email
=?";
31.
return qr.query(sql,new BeanHandler<User>(User.class),
email
);
32.
} catch (SQLException e) (
33.
throw new RuntimeException(e);
34.
}
35.
}
36.
/**
37.
*插入user
38.
**param user
39.
*/
40.
public void add(User user) {
41.
try {
42.
String sql = "insert into tb_user values(?,?,?,?,?,?)";
43.
Object[]params=(user.getUid(),user.getusername(),
44.
user.getPassword(),user.getEmail(),user.getCode(),
45.
user.isState()};
46.
qr.update(sql,parms);
47.
} catch(SQLException e) {
48.
throw new RuntimeException(e);
49.
}
50.
}
51.
}
Userservice,java:
1.
package cn.1tcast.pookstore.user.service;
2.
import cn.itcast.bookstore.user.dao.UserDao;
3.
import cn.itcast.bookstore.user.domain.User;
4.
/**
5.
*User业务层
6.
*@author cxf
7.
*
8.
*/
9.
public class UserService (
10.
private UserDao userDao = new UserDao();
11.
/**
12.
*注册功能
13.
* @param form
14.
*/
15.
public void regist(User form) throws UserException(
16.
//校验用户名
17.
User user = userDao.findByUsername(form.getUsername());if(usex!= null)throw new UserException("用户名已被注册!");
18.
//校验email
19.
user = userDao.findByEmail(form.getEmail());
20.
if(user != null)throw new UserException("Email已被注册!");
21.
//插入用户到数据库
22.
userDao.add(form);
23.
}
24.
}
UserServlet.java:
1.
package cn.itcast.bookstore.user.web.servlet;
2.
import java.io.IOException;
3.
/**
4.
*User表述层
5.
*/
6.
public class UserServlet extends BaseServlet {
7.
private UserService userService = new UserService();
8.
/**
9.
*注册功能
10.
* @param request
11.
* @param response
12.
* @return
13.
* @throws ServletException
14.
* @throws IOException
15.
*/
16.
public String regist(HttpServletRequest request, HttpservletResponse response)
17.
throws ServletException, IOException {
18.
/*
19.
*1.封装表单数据到form对象中
20.
*2.补全:uid、code
21.
*3.输入校验
22.
*>保存错误信息、form到request域,转发到regist.jsp
23.
*4.调用service方法完成注册
24.
*>保存错误信息、form到request域,转发到regist.jsp
25.
*5.发邮件
26.
*6.保存成功信息转发到msg.jsp
27.
*/
28.
//封装表单数据
29.
User form= CommonUtils.toBean(request.getParameterMap(),User.class);
30.
//补全
31.
form.setUid(CommonUtils.uuid());
32.
form.setcode(CommonUtils.uuid()+ CommonUtils.uuid());
33.
/*
34.
*输入校验
35.
*1.创建一个Map,用来封装错误信息,其中key为表单字段名称,值为错误信息
36.
*/
37.
Map<String,String> errors = new HashMap<String,String>();
38.
/*
39.
*2.获取form中的username、password、email进行校验
40.
*/
41.
String username = form.getUsername();
42.
if(username == null Il username.trim().isEmpty()){
43.
errors-put(”username”,"用户名不能为空!");
44.
} else if(username.length()< 3 Il username.length()> 10)(errors.put("username","用户名长度必须在3~10之间!");
45.
}
46.
String password = form.getPassword();
47.
if(password== null ‖password.trim().isEmpty()){
48.
errors-put("password”,"密码不能为空!");
49.
} else if(password.length()< 3 ‖password.length()> 10) {
50.
errors、put("passwoxd","密码长度必须在3~10间!"):
51.
}
52.
String email = form.getEmail();
53.
if(email == null ‖ email.trim().isEmpty()){
54.
errors.put(”email","Email不能为空!");
55.
} else if(email.matches("\\w+@\\w\\.\\w")){
56.
errors.put("emai1”,"Email格式错误!");
57.
}
58.
/*
59.
*3.判断是否存在错误信息
60.
*/
61.
if(errors.size()> 0){
62.
//1.保存错误信息
63.
//2.保存表单数据
64.
//3.转发到regist.jsp
65.
request.setAttribute("errors",errors);
66.
request.setAttribute("form",form);
67.
return "f:/jsps/user/regist.jsp";
68.
}
69.
/*
70.
*调用service的regist()方法
71.
*/
72.
try {
73.
userservice.regist(form);
74.
} catch (UserException e) {
75.
/*
76.
*1.保存异常信息
77.
*2.保存foxm
78.
*3.转发到regist.jsp
79.
*/
80.
request.setAttribute("msg",e.getMessage());
81.
request.setAttribute("form",form);
82.
return "f:/jsps/user/regist.jsp";
83.
}
84.
/*
85.
*发邮件
86.
*准备配置文件!
87.
*/
88.
//获取配置文件内容
89.
Properties props = new Properties();
90.
props.load(this.getClass().getClassLoader()
91.
·getResourceAsStream("email_template.properties"));
92.
string host=props.getproperty("host");//获取服务器主机
93.
string uname= props·getProperty("uname");//获取用户名
94.
string pwd= props.getProperty("pwd");//获取密码
95.
Stringfrom =props.getProperty("from");//获取发件人
96.
string to =form.getEmail();//获取收件人
97.
String subject三props.getProperty("subject");//获取主题
98.
string content=props.getProperty("content");//获取邮件内容
99.
content = MessageFormat.format(content,form.getcode());//替换{0}
100.
Session session=MailUtils.createSession(host,uname,pwd);//得到session
101.
Mail mail= new Mail(from,to,subject,content);//创建邮件对象
102.
try {
103.
MailUtils.send(session,mail);//发邮件!
104.
} catch (MessagingException e) {
105.
}
106.
/*
107.
*1.保存成功信息
108.
*2.转发到msg.jsp
109.
*/
110.
request.setAttribute("msg”,"恭喜,注册成功!请马上到邮箱激活");
111.
}
112.
}