账户注册激活邮件及登入和注销

一:创建实体类:

 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 }

Customer

二:创建注册界面:

 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>

RegistPage.jsp

 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>

LoginPage.jsp

三:创建控制器:

控制器:servlet:controller

 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 }

ControllerServlet

四:实体类DAO层:

 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 }

CustomerDaoMySQLImpl

五:完成

原文地址:https://www.cnblogs.com/biaogejiushibiao/p/9369059.html

时间: 2024-08-14 15:48:19

账户注册激活邮件及登入和注销的相关文章

javamail之实现注册激活邮件

需要的jar包 1.mysql驱动包 2.mail.jar包 SMTP和POP3协议概述 SMTP协议称为简单邮件传输协议,是一组用于从原地址到目的地址传送邮件的规则,由它来控制信件的中转方式.SMTP协议属于TCP/IP的协议簇,SMTP是负责邮件服务器之间的寄信的通信协定 POP3协议称为邮局协议版本3,也是TCP/IP协议簇的一员,基于POP3协议的服务器是用来接收信件的.每个Email地址一般只有一个如果想要同时收取多个邮箱的信件,就需要挨个设置每个邮箱的POP3服务器地址 核心代码实现

php用户登入与注销(cookie)

登入界面 <?php header('Content-type:text/html;charset=utf-8'); if(isset($_COOKIE['username']) && $_COOKIE['username']==='zeng'){ exit('您已经登入了,请不要重新登入'); } if(isset($_POST['submit'])){ if(isset($_POST['username']) && isset($_POST['password']

php用户登入与注销(session)

登入界面 <?php    session_start();    header('Content-type:text/html;charset=utf-8');       if(isset($_SESSION['username']) && $_SESSION['username']==='zeng'){        exit('您已经登入了,请不要重新登入');    }     if(isset($_POST['submit'])){        if(isset($_P

无需激活直接同步登入discuz,php代码(直接可用)

1 <?php 2 /** 3 * 抽奖 4 * @param int $total 5 */ 6 function getReward($total=1000) 7 { 8 $win1 = floor((0.12*$total)/100); 9 $win2 = floor((3*$total)/100); 10 $win3 = floor((12*$total)/100); 11 $other = $total-$win1-$win2-$win3; 12 $return = array();

第七十五天上课 php注册登入审核和文件上传

文件上传 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>文件上传</title> <?php if(!(empty($_FILES['file']) || empty($_POST['submit']))) { $urls="./my-img/".$_FILES['file']['name']; /

Java实现注册时发送激活邮件+激活

最近从项目分离出来的注册邮箱激活功能,整理一下,方便下次使用 1.RegisterController.java package com.app.web.controller; import java.text.ParseException; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; i

注册,激活邮件实践

本文参考十八哥的视频 数据库: create table user(uid int primary key auto_increment,uname char(20) not null default ' ',email char(32) not null default ' ',pass char(32) not null default '',status tinyint not null default 0)engine myisam charset utf8; create table

java 实现注册时发送激活邮件+激活

在很多网站注册的时候,为了验证用户信息的真实合法,往往需要验证用户所填邮件的准确性.形式为:用户注册时填写邮箱,注册完成后,网站会向用户所填邮箱发送一封激活邮件,用户点击激活邮件中的链接后,方可完成注册. 最近项目中也用到这个需求,做了个Demo与大家分享,大至思想如下: 数据库表结构 用户表t_user有五个字段分别为用户名.密码.邮箱地址.激活码.状态: | username | password | email |code | state | 核心代码: UserManager.java

LINUX下的远端主机登入 校园网络注册 网络数据包转发和捕获

第一部分:LINUX 下的远端主机登入和校园网注册 校园网内目的主机远程管理登入程序 本程序为校园网内远程登入,管理功能,该程序分服务器端和客户端两部分:服务器端为remote_server_udp.py 客户端分为单播客户端和广播客户端: 单播客户端client_unicast.py 广播客户端client_broadcast.py 1.单播客户端为根据net.info文件中的网络记录遍历目标网段中的所有IP,向其发送UDP封包. net.info中记录了目标网络中的一个样例IP和目标网段的子