JavaEE账号注册模拟网站邮箱激活
程序员文章站
2024-03-13 16:04:39
本项目是javaweb和email结合的项目,模拟网站必须要激活邮箱账号才可以使用,
创建数据库
create table user(
id varchar(...
本项目是javaweb和email结合的项目,模拟网站必须要激活邮箱账号才可以使用,
创建数据库
create table user( id varchar(32) primary key, name varchar(30), pwd varchar(20), email varchar(20), active char(1), acode varchar(32) )
user.java值对象
package cn.hncu.reg.domain; import javax.persistence.table; public class user { private string id; private string name; private string pwd; private string acode; private string active; private string email; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpwd() { return pwd; } public void setpwd(string pwd) { this.pwd = pwd; } public string getacode() { return acode; } public void setacode(string acode) { this.acode = acode; } public string getactive() { return active; } public void setactive(string active) { this.active = active; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashcode()); return result; } @override public boolean equals(object obj) { if (this == obj) return true; if (obj == null) return false; if (getclass() != obj.getclass()) return false; user other = (user) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }
c3p0pool.java
package cn.hncu.pubs; import java.sql.connection; import java.sql.sqlexception; import javax.sql.datasource; import com.mchange.v2.c3p0.combopooleddatasource; public class c3p0pool { private static datasource ds=new combopooleddatasource();; private static threadlocal<connection> tl=new threadlocal<connection>(); public static connection getcon() throws sqlexception { connection con = (connection)tl.get(); if (con == null) { con = ds.getconnection(); tl.set(con); } return con; } public static datasource getpool() { return ds; } public static threadlocal<connection> gettl() { return tl; } }
c3p0-config.xml
<c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config> <property name="driverclass">com.mysql.jdbc.driver</property> <property name="jdbcurl"> <![cdata[jdbc:mysql://127.0.0.1:3306/sstud?useunicode=true&characterencoding=utf-8]]> </property> <property name="user">root</property> <property name="password">1234</property> <!-- 初始化池大小 --> <property name="initialpoolsize">2</property> <!-- 最大空闲时间 --> <property name="maxidletime">30</property> <!-- 最多有多少个连接 --> <property name="maxpoolsize">10</property> <!-- 最少几个连接 --> <property name="minpoolsize">2</property> <!-- 每次最多可以执行多少个批处理语句 --> <property name="maxstatements">50</property> </default-config> <!-- 命名的配置 --> <named-config name="hncu"> <property name="driverclass">com.mysql.jdbc.driver</property> <property name="jdbcurl">jdbc:mysql://127.0.0.1:3306/sstud</property> <property name="user">root</property> <property name="password">1234</property> <property name="acquireincrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 --> <property name="initialpoolsize">100</property> <property name="minpoolsize">50</property> <property name="maxpoolsize">1000</property> <property name="maxstatements">0</property> <property name="maxstatementsperconnection">5</property> <!-- he's important, but there's only one of him --> </named-config> </c3p0-config>
regdao.java
package cn.hncu.reg.dao; import cn.hncu.reg.domain.user; public interface regdao { public user reg(user user); public user active(string acode); }
regdaojdbc.java
package cn.hncu.reg.dao; import java.sql.sqlexception; import java.util.uuid; import javax.sql.datasource; import org.apache.commons.dbutils.queryrunner; import org.apache.commons.dbutils.handlers.beanhandler; import org.apache.commons.dbutils.handlers.beanlisthandler; import cn.hncu.pool.c3p0pool; import cn.hncu.reg.domain.user; public class regdaojdbc implements regdao{ @override public user reg(user user) { datasource ds=c3p0pool.getpool(); queryrunner qr=new queryrunner(ds); string sql="insert into user(id,name,pwd,email,active,acode) values(?,?,?,?,?,?)"; try { string id=uuid.randomuuid().tostring().replaceall("-", ""); string acode=uuid.randomuuid().tostring().replaceall("-", ""); qr.update(sql,id,user.getname(),user.getpwd(),user.getemail(),"0",acode); user.setacode(acode); user.setid(id); user.setactive("0"); } catch (sqlexception e) { // e.printstacktrace(); system.out.println("注册失败"); return null; } return user; } @override public user active(string acode) { datasource ds=c3p0pool.getpool(); queryrunner qr=new queryrunner(ds); string sql="select * from user where acode=?"; user user=null; try { user=qr.query(sql, new beanhandler(user.class),acode); if(user!=null){ sql="update user set active='1' ,acode=''where acode=?"; qr.update(sql,acode); return user; } } catch (sqlexception e) { user=null; e.printstacktrace(); } return null; } }
iregservice.java
package cn.hncu.reg.service; import cn.hncu.reg.domain.user; public interface iregservice { public user reg(user user); public user active(string acode); }
regservice.java
package cn.hncu.reg.service; import cn.hncu.reg.dao.regdao; import cn.hncu.reg.dao.regdaojdbc; import cn.hncu.reg.domain.user; public class regservice implements iregservice { private regdao dao=new regdaojdbc(); @override public user reg(user user) { return dao.reg(user); } @override public user active(string acode) { return dao.active(acode); } }
regservlet.java
package cn.hncu.reg.servlet; 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; import cn.hncu.reg.domain.user; import cn.hncu.reg.sendmail.sendemailthread; import cn.hncu.reg.service.iregservice; import cn.hncu.reg.service.regservice; public class regservlet extends httpservlet { private iregservice service=new regservice(); public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); printwriter out = response.getwriter(); out.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">"); out.println("<html>"); out.println(" <head><title>a servlet</title></head>"); out.println(" <body>"); out.println("请按要求填写"); out.println(" </body>"); out.println("</html>"); out.flush(); out.close(); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); string name=request.getparameter("name"); string pwd=request.getparameter("pwd"); string email=request.getparameter("email"); user user=new user(); user.setname(name); user.setpwd(pwd); user.setemail(email); user=service.reg(user); if (user!=null) { out.print("注册成功,请到邮箱验证"); new sendemailthread(user.getacode()).start(); }else{ out.print("注册失败"); } } }
sendemailthread.java
package cn.hncu.reg.sendmail; import java.util.properties; import javax.mail.authenticator; import javax.mail.message.recipienttype; import javax.mail.messagingexception; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.addressexception; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import org.apache.log4j.logger; public class sendemailthread extends thread { private string acode; private logger log=logger.getlogger(sendemailthread.class); public sendemailthread(string acode) { this.acode = acode; } @override public void run() { properties p=new properties(); p.setproperty("mail.host", "smtp.sina.com"); p.setproperty("mail.smtp.auth", "true"); session session=session.getdefaultinstance(p, new authenticator() { @override protected passwordauthentication getpasswordauthentication() { passwordauthentication pa=new passwordauthentication("hncujava", "1234567"); return pa; } }); session.setdebug(true); mimemessage msg=new mimemessage(session); try { msg.setfrom( new internetaddress("hncujava@sina.com") ); msg.setrecipient(recipienttype.to, new internetaddress("747205398@qq.com")); msg.setsubject("激活hncu账号"); stringbuilder sb=new stringbuilder(); sb.append("请激活邮箱:<a href='http://127.0.0.1:8080/mailweb/activeservlet"); sb.append("?acode="+acode+" '>点击激活</a>"); sb.append("<br/>如果激活未成功,请复制下面地址到浏览器地址栏进行手动激活:"); sb.append("http://www.hncu.cn/mailweb/activeservlet?acode="+acode); msg.setcontent(sb.tostring(),"text/html;charset=utf-8"); transport.send(msg); log.info("邮件发送成功"); } catch (addressexception e) { log.error("邮箱地址错误,邮件发送失败"); e.printstacktrace(); } catch (messagingexception e) { e.printstacktrace(); log.error("邮箱信息格式错误,邮件发送失败"); } } }
activeservlet.java
package cn.hncu.reg.servlet; 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; import cn.hncu.reg.domain.user; import cn.hncu.reg.service.iregservice; import cn.hncu.reg.service.regservice; public class activeservlet extends httpservlet { private iregservice service=new regservice(); public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); string acode=request.getparameter("acode"); user user=service.active(acode); request.setattribute("user", user); system.out.println("user:"+user); request.getrequestdispatcher("/jsps/result.jsp").forward(request, response); } }
index.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>邮箱登录</title> <script type="text/javascript"> function reg(){ document.forms[0].action='<c:url value="/jsps/reg.jsp"/>'; document.forms[0].submit(); } function login(){ document.forms[0].action='<c:url value="/loginservlet"/>'; document.forms[0].submit(); } </script> </head> <body> <c:if test="${!empty error }"> 登录失败 <c:remove var="error" scope="session"/> </c:if> <c:if test="${empty sessionscope.user }" var="boo"> <h3>用户注册</h3> <form action="" method="post" > 姓名:<input type="text" name="name" value="wang"/><br/> 密码:<input type="text" name="pwd" value="1234"/><br/> <input type="button" value="登录" onclick="login();"/> <input type="button" value="注册" onclick="reg();"/><br/> </form> </c:if> <c:if test="${!boo }"> 欢迎回来,${ sessionscope.user.name} </c:if> </body> </html>
reg.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>邮箱注册系统</title> <script type="text/javascript"> function reg(){ document.forms[0].action='<c:url value="/regservlet"/>'; document.forms[0].submit(); } function login(){ document.forms[0].action='<c:url value="/loginservlet"/>'; document.forms[0].submit(); } </script> </head> <body> <c:if test="${empty sessionscope.user }" var=" boo"> <h3>用户注册</h3> <form action="" method="post" > 姓名:<input type="text" name="name" value="wang"/><br/> 密码:<input type="text" name="pwd" value="1234"/><br/> 邮箱:<input type="text" name="email" value="747205398@qq.com"/> <br/> <input type="button" value="注册" onclick="reg();"/><br/> </form> </c:if> </body> </html>
result.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <script type="text/javascript"> var tm; var time=5; function start(){ div1.innerhtml="将在"+time+"秒跳转"; time--; if(time<=1){ window.clearinterval(tm); window.location.href="<c:url value='/index.jsp'/>"; } } onload=function(){ tm=window.setinterval(start, 1000); }; </script> </head> <body> <c:if test="${!empty requestscope.user }" var=" boo"> 恭喜注册成功!请登录 <a href='<c:url value="/index.jsp" />'>登录</a><br/><br/> <div id ="div1"> </div> </c:if> <c:if test="${boo}"> 注册失败,请重新激活 </c:if> </body> </html>
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <filter> <filter-name>charset</filter-name> <filter-class>cn.hncu.filter.characterfilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charset</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <description>this is the description of my j2ee component</description> <display-name>this is the display name of my j2ee component</display-name> <servlet-name>regservlet</servlet-name> <servlet-class>cn.hncu.reg.servlet.regservlet</servlet-class> </servlet> <servlet> <description>this is the description of my j2ee component</description> <display-name>this is the display name of my j2ee component</display-name> <servlet-name>activeservlet</servlet-name> <servlet-class>cn.hncu.reg.servlet.activeservlet</servlet-class> </servlet> <servlet> <description>this is the description of my j2ee component</description> <display-name>this is the display name of my j2ee component</display-name> <servlet-name>loginservlet</servlet-name> <servlet-class>cn.hncu.login.loginservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>regservlet</servlet-name> <url-pattern>/regservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>activeservlet</servlet-name> <url-pattern>/activeservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>loginservlet</servlet-name> <url-pattern>/loginservlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
需要的jar包
c3p0-0.9.1.2.jar
commons-dbutils-1.4.jar
java-mail-1.4.4.jar
mysql-connector-java-5.1.34-bin.jar
myconutil.jar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: JavaEE账号注册模拟网站邮箱激活