zookeeper python接口实例详解
301
2023-07-06
JavaEE账号注册模拟网站邮箱激活
本项目是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
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
return tl;
}
}
C3p0-config.xml
<![CDATA[jdbc:mysql://127.0.0.1:3306/sstud?useUnicode=true&characterEncoding=UTF-8]]>
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(" ");
out.println("");
out.println("
out.println("
out.println("请按要求填写");
out.println(" ");
out.println("");
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("请激活邮箱:点击激活");
sb.append("
如果激活未成功,请复制下面地址到浏览器地址栏进行手动激活:");
sb.append("http://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"%>
function reg(){
document.forms[0].action='
document.forms[0].submit();
}
function login(){
document.forms[0].action='
document.forms[0].submit();
}
登录失败
姓名:
密码:
欢迎回来,${ sessionScope.user.name}
reg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
function reg(){
document.forms[0].action='
document.forms[0].submit();
}
function login(){
document.forms[0].action='
document.forms[0].submit();
}
姓名:
密码:
邮箱:
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
var tm;
var time=5;
function start(){
div1.innerHTML="将在"+time+"秒跳转";
time--;
if(time<=1){
window.clearInterval(tm);
window.location.href="
}
}
onload=function(){
tm=window.setInterval(start, 1000);
};
恭喜注册成功!请登录
注册失败,请重新激活
web.xml
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://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"> RlHxIaU
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://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">
RlHxIaU
需要的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
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~