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 tl=new 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 getTl() {
    return tl;
  }
}

C3p0-config.xml


  
  
    com.mysql.jdbc.Driver
    
      
    
    root
    1234
    
    2
    
    30
    
    10
    
    2
    
    50
   
  
  
    com.mysql.jdbc.Driver
    jdbc:mysql://127.0.0.1:3306/sstud
    root
    1234
    5
    100
    50
    1000
    0
    5 
  


 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(" A Servlet");
    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("[email protected]") );
      msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
      msg.setSubject("激活hncu账号");
      StringBuilder sb=new StringBuilder();
      sb.append("请激活邮箱:点击激活");
      sb.append("
如果激活未成功,请复制下面地址到浏览器地址栏进行手动激活:"); 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"%>



 
  邮箱登录
  
 

 
 
  登录失败
  
 
 
    

用户注册

姓名:
密码:

欢迎回来,${ 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"%>



 
  邮箱注册系统
  
 

 
 
    

用户注册

姓名:
密码:
邮箱:

result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



 
  
 

 
 
    恭喜注册成功!请登录
    登录

注册失败,请重新激活

web.xml



 

 
  charset
  cn.hncu.filter.CharacterFilter
  
    charset
    utf-8
  
 

 
  charset
  /*
 
 
  This is the description of my J2EE component
  This is the display name of my J2EE component
  RegServlet
  cn.hncu.reg.servlet.RegServlet
 
 
  This is the description of my J2EE component
  This is the display name of my J2EE component
  ActiveServlet
  cn.hncu.reg.servlet.ActiveServlet
 
 
  This is the description of my J2EE component
  This is the display name of my J2EE component
  LoginServlet
  cn.hncu.login.LoginServlet
 


 
  RegServlet
  /RegServlet
 
 
  ActiveServlet
  /ActiveServlet
 
 
  LoginServlet
  /LoginServlet
 
 
  index.jsp
 


需要的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账号注册模拟网站邮箱激活)