天天看點

使用者子產品之注冊功能完成|學習筆記

開發者學堂課程【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. 

}