一.先说思路
//1.数据库加三个字段,state:(0:未激活,1:激活成功),acticode:(放激活码),token_exptime(过期时间,用来验证激活邮件是否过期)
//2.用户填写资料,点击注册,插入数据成功,state字段默认是0,同时生成一个acticode(用传过来的邮箱、密码、和当前时间加密形成)也存入数据库
//3.发送邮件。。。提示用户登录邮箱激活。。。邮件中带一个激活成功页的url,url里有两个参数(1,用户id,2:激活码)
//4.用户登录邮箱点击链接,来到处理激活的业务逻辑页面或servlet,得到url中两个参数,以这两个参数为条件查询数据库里的数据,如果有,取当前时间和之前存入数据库的过期时间作比较,看是否过期,过期,删除数据库中该条记录,并转到失败页面,没过期,查看链接传过来的激活码与数据库字段激活码是否一致,不一致,同样删除数据库中该条记录,并跳转到激活失败界面,一致,则将字段state为1,激活成功,转到激活成功页。。。
二、具体实现代码
1.首先,准备一个简单的测试页面
<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,注意带的参数op指我要做的操作,用于后面的servlet做判断该做什么操作,下面的代码完成了形成激活码、过期时间等表示当前注册用户的状态的信息存入数据库并发送邮件的过程。(邮件内容自定义,可以忽略我的)
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是什么,调用不同的方法做处理 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.数据库加两个字,state字段(0:未激活,1:激活成功),acticode:(放激活码) // 2.用户填写资料,插入数据成功,state字段默认是0,同时生成一个acticode也存入数据库 // 3.提示用户激活。。。发送邮件。。。邮件中带一个激活成功页的url,url里有两个参数(1,用户id,2:激活码) // 4.用户点击链接,回到激活成功页。。。激活成功页的load事件,得到两个参数,以这两个参数为条件查询数据库里的数据,如果有,修改字段state为1,反之。。提示激活失败,重新激活。。 string email=request.getparameter(“email”); string pwd=encrypt.md5(request.getparameter(“pwd”)); calendar c = calendar.getinstance(); //现在的时间(单位:毫秒) //todo:时间换算问题,如何处理int和long之间的关系 long time = c.gettimeinmillis(); //创建激活码 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 ){ //发送邮件 ///邮件的内容 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是一款”专注兴趣、分享创作”的轻博客产品,旨在为”热爱记录生活、追求时尚品质、崇尚自由空间”的你,打造一个全新而定展示平台!<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/>如果以上链接无法点击,请把上面网页地址复制到浏览器地址栏中打开<br/><br/><br/>lofter,专注兴趣,分享创作<br/>” sdf.format(new date()) “</div></div>” ); //发送邮件 sendemail.send(email, sb.tostring()); } response.sendredirect(“doemail.action?op=emaillogin&email=” email “&pwd=” pwd); } }
这是qq邮箱设置,一般开启1、2、4
这是收到的激活邮件
根据用户所填邮箱跳转相应邮箱登录地址的代码
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是什么,调用不同的方法做处理 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是什么,调用不同的方法做处理 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 { //获取参数token的值,即激活识别码。 //将它与数据表中的用户信息进行查询对比,如果有相应的数据集,判断是否过期,如果在有效期内则将对应的用户表中字段status设置1,即已激活,这样就完成了激活功能。 string id=request.getparameter(“id”); string token=request.getparameter(“token”); calendar c = calendar.getinstance(); //现在的时间(单位:毫秒) 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 ){ //激活码过期,先删除该用户记录,然后重新发送邮件 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())){ //激活成功, //并更新用户的激活状态,为已激活 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; // –激活状态 ,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; } }
如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。
西部数码优质企业邮箱服务商,提供安全稳定,简单易用,高性价比的企业邮箱。按需自由定制,不限空间,极速收发,能够满足用户对企业邮箱的不同需求。多种反垃圾邮件算法,99.9%精准度,智能过滤,减少垃圾邮件干扰。支持小程序收发邮件,随时随地移动办公。而且价格实惠,还可以免费试用,7×24小时专业团队服务支持!
高性价比企业邮箱开通链接:https://www.west.cn/services/mail/
软文推广如何激发用户的购买欲望玩具网站建设该怎么做才有助于发展何为国际域名?国际域名有什么用?我申请了转出域名对方在其他平台提交了以后我在后台操作了同意但阿里云租用虚拟服务器电脑中打开屏幕录像文件提示没有找到需要的MP3解码器怎么办云服务器可以建站吗电脑显示不出u盘怎么办 u盘插在电脑上找不到怎么解决