账户注册激活邮件及登入和注销
一:创建实体类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.Serializable; 2 3 public class Customer implements Serializable { 4 private String id; 5 private String username; 6 private String password; 7 private String phone; 8 private String address; 9 private String email; 10 private boolean actived;//是否激活 11 private String code;//激活码 12 public String getId() { 13 return id; 14 } 15 public void setId(String id) { 16 this.id = id; 17 } 18 public String getUsername() { 19 return username; 20 } 21 public void setUsername(String username) { 22 this.username = username; 23 } 24 public String getPassword() { 25 return password; 26 } 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 public String getPhone() { 31 return phone; 32 } 33 public void setPhone(String phone) { 34 this.phone = phone; 35 } 36 public String getAddress() { 37 return address; 38 } 39 public void setAddress(String address) { 40 this.address = address; 41 } 42 public String getEmail() { 43 return email; 44 } 45 public void setEmail(String email) { 46 this.email = email; 47 } 48 public boolean isActived() { 49 return actived; 50 } 51 public void setActived(boolean actived) { 52 this.actived = actived; 53 } 54 public String getCode() { 55 return code; 56 } 57 public void setCode(String code) { 58 this.code = code; 59 } 60 61 }
二:创建注册界面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <h1>新用户注册</h1> 2 <form action="${pageContext.request.contextPath}/servlet/ControllerServlet?op=regist" method="post"> 3 <table border="1" width="438"> 4 <tr> 5 <td>(*)用户名:</td> 6 <td> 7 <input type="text" name="username"/> 8 </td> 9 </tr> 10 <tr> 11 <td>(*)密码:</td> 12 <td> 13 <input type="password" name="password"/> 14 </td> 15 </tr> 16 <tr> 17 <td>重复密码:</td> 18 <td> 19 <input type="password" name="repassword"/> 20 </td> 21 </tr> 22 <tr> 23 <td>(*)电话:</td> 24 <td> 25 <input type="text" name="phone"/> 26 </td> 27 </tr> 28 <tr> 29 <td>(*)收货地址:</td> 30 <td> 31 <input type="text" name="address"/> 32 </td> 33 </tr> 34 <tr> 35 <td>(*)邮箱:</td> 36 <td> 37 <input type="text" name="email"/> 38 </td> 39 </tr> 40 <tr> 41 <td colspan="2"> 42 <input type="submit" value="注册"/> 43 </td> 44 </tr> 45 </table> 46 </form>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <h1>用户登入</h1> 2 <form action="${pageContext.request.contextPath}/servlet/ControllerServlet?op=login" method="post"> 3 <table border="1" width="438"> 4 <tr> 5 <td>(*)用户名:</td> 6 <td> 7 <input type="text" name="username"/> 8 </td> 9 </tr> 10 <tr> 11 <td>(*)密码:</td> 12 <td> 13 <input type="password" name="password"/> 14 </td> 15 </tr> 16 <tr> 17 <td colspan="2"> 18 <input type="submit" value="登录"/> 19 </td> 20 </tr> 21 </table> 22 </form>
三:创建控制器:
控制器:servlet:controller
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class ControllerServlet extends HttpServlet { 2 private BusinessService s = new BusinessServiceImpl(); 3 public void doGet(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 String op = request.getParameter("op"); 6 if("regist".equals(op)){ 7 regist(request,response); 8 }else if("login".equals(op)){ 9 login(request,response); 10 }else if("logout".equals(op)){ 11 logout(request,response); 12 }else if("active".equals(op)){ 13 active(request,response); 14 } 15 } 16 //激活账户 17 private void active(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 18 String username = request.getParameter("username"); 19 String code = request.getParameter("code"); 20 Customer c = s.findCustomer(username, code); 21 if(c==null){ 22 response.getWriter().write("激活失败"); 23 return; 24 } 25 c.setActived(true); 26 s.updateCustomer(c); 27 response.getWriter().write("<script type='text/javascript'>alert('激活成功')</script>"); 28 response.setHeader("Refresh", "0;URL="+request.getContextPath()); 29 } 30 //注销 31 private void logout(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 32 request.getSession().removeAttribute("customer"); 33 response.sendRedirect(request.getContextPath());//重定向到主页 34 } 35 //登录 36 private void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 37 String username = request.getParameter("username"); 38 String password = request.getParameter("password"); 39 Customer c = s.login(username, password); 40 if(c==null){ 41 response.getWriter().write("<script type='text/javascript'>alert('登录失败')</script>"); 42 response.setHeader("Refresh", "0;URL="+request.getContextPath()+"/login.jsp"); 43 return; 44 } 45 request.getSession().setAttribute("customer", c); 46 response.sendRedirect(request.getContextPath());//重定向到主页 47 } 48 private void regist(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 49 Customer c = WebUtil.fillBean(request, Customer.class); 50 //生成随机验证码 51 String code = UUID.randomUUID().toString(); 52 c.setCode(code); 53 //单独启动一个线程:发送激活邮件 54 SendMail sm = new SendMail(c); 55 sm.start(); 56 s.regist(c); 57 response.getWriter().write("<script type='text/javascript'>alert('注册成功!我们已经发送了一封激活邮件到您的邮箱,请注意查查')</script>"); 58 response.setHeader("Refresh", "0;URL="+request.getContextPath()); 59 } 60 public void doPost(HttpServletRequest request, HttpServletResponse response) 61 throws ServletException, IOException { 62 doGet(request, response); 63 } 64 65 }
四:实体类DAO层:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.BeanHandler; 3 4 import com.itheima.dao.CustomerDao; 5 import com.itheima.domain.Customer; 6 import com.itheima.util.DBCPUtil; 7 8 public class CustomerDaoMySQLImpl implements CustomerDao { 9 private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource()); 10 public void save(Customer c) { 11 try{ 12 qr.update("insert into customer (id,username,password,phone,address,email,actived,code) values(?,?,?,?,?,?,?,?)", 13 c.getId(),c.getUsername(),c.getPassword(), 14 c.getPhone(),c.getAddress(),c.getEmail(), 15 c.isActived(),c.getCode()); 16 }catch(Exception e){ 17 throw new RuntimeException(e); 18 } 19 } 20 21 public Customer find(String username, String password) { 22 try{ 23 return qr.query("select * from customer where username=? and password=?", new BeanHandler<Customer>(Customer.class), username,password); 24 }catch(Exception e){ 25 throw new RuntimeException(e); 26 } 27 } 28 29 public Customer findByCode(String username, String code) { 30 try{ 31 return qr.query("select * from customer where username=? and code=?", new BeanHandler<Customer>(Customer.class), username,code); 32 }catch(Exception e){ 33 throw new RuntimeException(e); 34 } 35 } 36 37 public Customer findById(String customerId) { 38 try{ 39 return qr.query("select * from customer where id=?", new BeanHandler<Customer>(Customer.class), customerId); 40 }catch(Exception e){ 41 throw new RuntimeException(e); 42 } 43 } 44 45 public void update(Customer c) { 46 try{ 47 qr.update("update customer set username=?,password=?,phone=?,address=?,email=?,actived=?,code=? where id=?", 48 c.getUsername(),c.getPassword(), 49 c.getPhone(),c.getAddress(),c.getEmail(), 50 c.isActived(),c.getCode(),c.getId()); 51 }catch(Exception e){ 52 throw new RuntimeException(e); 53 } 54 } 55 56 }
五:完成
合群是堕落的开始 优秀的开始是孤行