//1.數(shù)據(jù)庫加三個字段,state:(0:未激活,1:激活成功),ActiCode:(放激活碼),token_exptime(過期時間,用來驗證激活郵件是否過期)

//2.用戶填寫資料,點擊注冊,插入數(shù)據(jù)成功,state字段默認(rèn)是0,同時生成一個ActiCode(用傳過來的郵箱、密碼、和當(dāng)前時間加密形成)也存入數(shù)據(jù)庫
//3.發(fā)送郵件。。。提示用戶登錄郵箱激活。。。郵件中帶一個激活成功頁的URL,URL里有兩個參數(shù)(1,用戶ID,2:激活碼)
//4.用戶登錄郵箱點擊鏈接,來到處理激活的業(yè)務(wù)邏輯頁面或Servlet,得到URL中兩個參數(shù),以這兩個參數(shù)為條件查詢數(shù)據(jù)庫里的數(shù)據(jù),如果有,取當(dāng)前時間和之前存入數(shù)據(jù)庫的過期時間作比較,看是否過期,過期,刪除數(shù)據(jù)庫中該條記錄,并轉(zhuǎn)到失敗頁面,沒過期,查看鏈接傳過來的激活碼與數(shù)據(jù)庫字段激活碼是否一致,不一致,同樣刪除數(shù)據(jù)庫中該條記錄,并跳轉(zhuǎn)到激活失敗界面,一致,則將字段state為1,激活成功,轉(zhuǎn)到激活成功頁。。。
二、具體實現(xiàn)代碼
1.首先,準(zhǔn)備一個簡單的測試頁面

<body> <div id=”main” style=”margin:0 auto;width:500px;”> <form id=”reg” action=”user.action?op=reg” method=”post”> <p> E-mail:<input type=”text” class=”input” name=”email” id=”email”> </p> <p> 密 碼:<input type=”password” class=”input” name=”pwd” id=”pwd”> </p> <p> <input type=”submit” class=”btn” value=”提交注冊” > </p> </form> </div> </body>

2.點擊提交注冊,來到user.action?op=reg,注意帶的參數(shù)op指我要做的操作,用于后面的Servlet做判斷該做什么操作,下面的代碼完成了形成激活碼、過期時間等表示當(dāng)前注冊用戶的狀態(tài)的信息存入數(shù)據(jù)庫并發(fā)送郵件的過程。(郵件內(nèi)容自定義,可以忽略我的)

package com.nh.web.servlets; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.UUID; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.nh.dao.DBHelper; import com.nh.utils.Encrypt; import com.nh.utils.SendEmail; public class UserServlet extends CommonServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 取出op String op = request.getParameter(“op”); // 判斷op是什么,調(diào)用不同的方法做處理 try { if (op != null && !””.equals(op)) { if (“reg”.equals(op)) { regOP(request, response); } } else { } } catch (Exception e) { e.printStackTrace(); response.sendRedirect(“common/500.jsp”); } } private void regOP(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, NamingException { // 1.數(shù)據(jù)庫加兩個字,state字段(0:未激活,1:激活成功),ActiCode:(放激活碼) // 2.用戶填寫資料,插入數(shù)據(jù)成功,state字段默認(rèn)是0,同時生成一個ActiCode也存入數(shù)據(jù)庫 // 3.提示用戶激活。。。發(fā)送郵件。。。郵件中帶一個激活成功頁的URL,URL里有兩個參數(shù)(1,用戶ID,2:激活碼) // 4.用戶點擊鏈接,回到激活成功頁。。。激活成功頁的Load事件,得到兩個參數(shù),以這兩個參數(shù)為條件查詢數(shù)據(jù)庫里的數(shù)據(jù),如果有,修改字段state為1,反之。。提示激活失敗,重新激活。。 String email=request.getParameter(“email”); String pwd=Encrypt.md5(request.getParameter(“pwd”)); Calendar c = Calendar.getInstance(); //現(xiàn)在的時間(單位:毫秒) //TODO:時間換算問題,如何處理int和long之間的關(guān)系 long time = c.getTimeInMillis(); //創(chuàng)建激活碼 String token=Encrypt.md5(email pwd time); //過期時間為24小時后 // int token_exptime=(int)(time 1000*60*60*24); String token_exptime=(time 1000*20) “”; //這里測試是用的20秒 String id=UUID.randomUUID().toString(); String sql=”insert into tb_user(id,username,pwd,token,token_exptime,regtime,status) values (?,?,?,?,?,sysdate,0)”; List<Object> params=new ArrayList<Object>(); params.add(id); params.add(email); params.add(pwd); params.add(token); params.add(token_exptime); DBHelper db=new DBHelper(); int r=db.doUpdate(sql, params); //保存注冊信息 if( r>0 ){ //發(fā)送郵件 ///郵件的內(nèi)容 SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”); StringBuffer sb=new StringBuffer(“<div style=”width:660px;overflow:hidden;border-bottom:1px solid #bdbdbe;”><div style=”height:52px;overflow:hidden;border:1px solid #464c51;background:#353b3f url(http://www.lofter.com/rsc/img/email/hdbg.png);”><a href=”http://www.lofter.com?mail=qbclickbynoticemail_20120626_01″ target=”_blank” style=”display:block;width:144px;height:34px;margin:10px 0 0 20px;overflow:hidden;text-indent:-2000px;background:url(http://www.lofter.com/rsc/img/email/logo.png) no-repeat;”>LOFTER</a></div>” “<div style=”padding:24px 20px;”>您好,” email “<br/><br/>LOFTER是一款”專注興趣、分享創(chuàng)作”的輕博客產(chǎn)品,旨在為”熱愛記錄生活、追求時尚品質(zhì)、崇尚自由空間”的你,打造一個全新而定展示平臺!<br/><br/>請點擊下面鏈接激活賬號,24小時生效,否則重新注冊賬號,鏈接只能使用一次,請盡快激活!</br>”); sb.append(“<a href=”http://localhost:8080/mailtest/emailcheck.action?op=activate&id=”); sb.append(id); sb.append(“&token=”); sb.append(token); sb.append(“”>http://localhost:8080/mailtest/emailcheck.action?op=activate&id=”); sb.append(id); sb.append(“&token=”); sb.append(token); sb.append(“</a>” “<br/>如果以上鏈接無法點擊,請把上面網(wǎng)頁地址復(fù)制到瀏覽器地址欄中打開<br/><br/><br/>LOFTER,專注興趣,分享創(chuàng)作<br/>” sdf.format(new Date()) “</div></div>” ); //發(fā)送郵件 SendEmail.send(email, sb.toString()); } response.sendRedirect(“doEmail.action?op=emaillogin&email=” email “&pwd=” pwd); } }

這是QQ郵箱設(shè)置,一般開啟1、2、4

這是收到的激活郵件

根據(jù)用戶所填郵箱跳轉(zhuǎn)相應(yīng)郵箱登錄地址的代碼

package com.nh.web.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DoEmailLoginServlet extends CommonServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String op = request.getParameter(“op”); // 判斷op是什么,調(diào)用不同的方法做處理 try { if (op != null && !””.equals(op)) { if (“emaillogin”.equals(op)) { emailloginOP(request, response); } } else { } } catch (Exception e) { e.printStackTrace(); response.sendRedirect(“common/500.jsp”); } } private void emailloginOP(HttpServletRequest request, HttpServletResponse response) throws IOException { //判斷用戶郵箱是什么,跳到指定郵箱登陸界面 String email=request.getParameter(“email”); //572480349@qq.com String pwd=request.getParameter(“pwd”); String addrstr=email.split(“@”)[1]; //qq.com if( “qq.com”.equals(addrstr)){ addrstr=”https://mail.qq.com”; }else if( “163.com”.equals(addrstr)){ addrstr=”http://mail.163.com/”; }else if( “126.com”.equals(addrstr)){ addrstr=”http://www.126.com/”; }else if( “sina.com”.equals(addrstr)){ addrstr=”http://mail.sina.com.cn/”; }else if( “hotmail.com”.equals(addrstr)){ addrstr=”https://login.live.com”; } response.sendRedirect(“emailaction.jsp?email=” email “&pwd=” pwd “&addrstr=” addrstr); } }

具體頁面我就不一一給了,直接上激活驗證代碼吧

package com.nh.web.servlets; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.lofter.bean.User; import com.nh.dao.DBHelper; import com.nh.utils.DataExistAlreadyException; public class EmailActivateCheckServlet extends CommonServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 取出op String op = request.getParameter(“op”); // 判斷op是什么,調(diào)用不同的方法做處理 try { if (op != null && !””.equals(op)) { if( “activate”.equals(op)){ activateOP(request,response); } } else { } } catch (Exception e) { e.printStackTrace(); response.sendRedirect(“common/500.jsp”); } } private void activateOP(HttpServletRequest request, HttpServletResponse response) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, Exception { //獲取參數(shù)token的值,即激活識別碼。 //將它與數(shù)據(jù)表中的用戶信息進(jìn)行查詢對比,如果有相應(yīng)的數(shù)據(jù)集,判斷是否過期,如果在有效期內(nèi)則將對應(yīng)的用戶表中字段status設(shè)置1,即已激活,這樣就完成了激活功能。 String id=request.getParameter(“id”); String token=request.getParameter(“token”); Calendar c = Calendar.getInstance(); //現(xiàn)在的時間(單位:毫秒) long curtime = c.getTimeInMillis(); String sql=”select id,token_exptime,token,username,pwd from tb_user where status=0 and token=?”; List<Object> params=new ArrayList<Object>(); params.add( token ); DBHelper db=new DBHelper(); User u=db.findSingleObject(User.class, sql, params); String email=u.getUsername(); String pwd=u.getPwd(); if( u!=null ){ long token_exptime=Long.parseLong(u.getToken_exptime()); if( curtime>token_exptime ){ //激活碼過期,先刪除該用戶記錄,然后重新發(fā)送郵件 sql=”delete from tb_user where id=\\\'” u.getId() “\\\'”; db.doUpdate(sql, null); response.sendRedirect(“actionfailer.jsp?email=” email “&pwd=” pwd); // throw new DataExistAlreadyException(“激活碼已過期!”); return; }else{ //驗證激活碼是否正確 if( token.equals(u.getToken())){ //激活成功, //并更新用戶的激活狀態(tài),為已激活 sql=”update tb_user set status=1 where id=\\\'” u.getId() “\\\'”; db.doUpdate(sql, null); response.sendRedirect(“actionsuccess.jsp”); }else{ sql=”delete from tb_user where id=\\\'” u.getId() “\\\'”; db.doUpdate(sql, null); response.sendRedirect(“actionfailer.jsp?email=” email “&pwd=” pwd); return; // throw new DataExistAlreadyException(“激活碼不正確”); } } } } } package com.nh.web.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public abstract class CommonServlet extends HttpServlet { private static final long serialVersionUID = 3893961453320250657L; private String saveFilePath=””; protected String basePath=””; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { saveFilePath=req.getRealPath(“/”); HttpSession session=req.getSession(); ServletContext application=session.getServletContext(); if( application.getAttribute(“basePath”)!=null ){ basePath=(String) application.getAttribute(“basePath”); } super.service(req, resp); } } package com.lofter.bean; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private static final long serialVersionUID = -1989259749641485708L; private String id; private String username; // –賬戶 private String pwd; // –密碼 private String nickname; // –名稱 private String autograph; // –個人簽名 private String head; // –頭像 private Date regtime; // –注冊時間 private String token; // –賬號激活碼 private String token_exptime; // –激活碼有效期 private Integer status; // –激活狀態(tài) ,0-未激活,1-已激活 public User() { super(); } public User(String id, String username, String pwd, String nickname, String autograph, String head, Date regtime, String token, String token_exptime, Integer status) { super(); this.id = id; this.username = username; this.pwd = pwd; this.nickname = nickname; this.autograph = autograph; this.head = head; this.regtime = regtime; this.token = token; this.token_exptime = token_exptime; this.status = status; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getAutograph() { return autograph; } public void setAutograph(String autograph) { this.autograph = autograph; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public Date getRegtime() { return regtime; } public void setRegtime(Date regtime) { this.regtime = regtime; } public static long getSerialversionuid() { return serialVersionUID; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public String getToken_exptime() { return token_exptime; } public void setToken_exptime(String token_exptime) { this.token_exptime = token_exptime; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } }

如果,你對上面的內(nèi)容還有疑問,推薦選擇西部數(shù)碼企業(yè)云郵箱!有專人協(xié)助您解答郵箱疑問。

西部數(shù)碼優(yōu)質(zhì)企業(yè)郵箱服務(wù)商,提供安全穩(wěn)定,簡單易用,高性價比的企業(yè)郵箱。按需自由定制,不限空間,極速收發(fā),能夠滿足用戶對企業(yè)郵箱的不同需求。多種反垃圾郵件算法,99.9%精準(zhǔn)度,智能過濾,減少垃圾郵件干擾。支持小程序收發(fā)郵件,隨時隨地移動辦公。而且價格實惠,還可以免費試用,7×24小時專業(yè)團(tuán)隊服務(wù)支持!

高性價比企業(yè)郵箱開通鏈接:http://bingfeng168.cn/services/mail/

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-62778877-8306;郵箱:fanjiao@west.cn。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:西部數(shù)碼知識庫 » 注冊郵箱驗證激活技術(shù)

登錄

找回密碼

注冊