JavaEE账号注册模拟网站邮箱激活_java

本项目是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账号注册
JavaEE邮箱激活
激活锁不记得邮箱账号、如何激活qq邮箱账号、怎么激活邮箱账号、如何激活邮箱账号、邮箱激活账号,以便于您获取更多的相关知识。

时间: 2024-10-12 00:47:11

JavaEE账号注册模拟网站邮箱激活_java的相关文章

Java注册邮箱激活验证实现代码_java

最近从项目分离出来的注册邮箱激活功能,整理一下,方便下次使用 RegisterValidateService.java 复制代码 代码如下: package com.app.service.impl; import java.text.ParseException;import java.util.Date; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stere

iPad中iCloud账号注册和激活教程

iPad中iCloud账号注册和激活 第一步 进入设置,选择iCloud 第二步 选择"创建新Apple ID",已经有的请直接登录吧. 第三步 店面一般选中国,然后点"下一步" 第四步 选择"同意" 第五步 根据下面的图,填写好注册的信息,点击"下一步" 第六步 填写好付款相关信息,点击"下一步" 第七步 点击"完成" 第八步 登录你注册时候的邮箱,打开激活邮件,点击"立刻激

账号通过邮箱激活的问题

问题描述 账号通过邮箱激活的问题 账号通过邮箱激活的问题,假如每天有十万用户注册,而邮箱的每天发邮件数量只有100封 怎么解决这个问题? 解决方案 邮件激活账号 解决方案二: 这...有毒啊,怎么破, 解决方案三: 因为你发的邮件太多了,服务器给你剁了. 这么多邮件,要买smtp服务

PHP+jQuery 注册模块的改进(二):邮箱激活第1/2页_php实例

接<PHP+jQuery 注册模块的改进之一>继续修改: ①在注册成功后返回登录邮件页面( maillogin.php ),在页面中用户可以点击链接跳转到自己注册邮箱的登录页面,可以再次发送激活链接: ②给激活链接设置有效期,过了有效期点击激活连接,会跳转页面让用户选择再次发送邮件或者返回注册页:再次发送邮件后,进邮箱点击链接激活: ③点击重新发送激活连接,不跳转页面,在当前页无刷新弹出通知:已重新发送链接,几秒钟之后通知消失: ④点击更换收件邮箱,跳转至注册页 1.点击邮箱链接跳转至相应邮箱

用户注册的邮箱激活模块的设计与实现

----------------------------------------------------------------------------------------------[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52144698作者:朱培      ID:sdksdk0      邮箱: zhupei@tianfang1314.cn    ------------------

【JavaMailWeb】用户注册通过邮箱激活案例

转载请注明出处: http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 前面刚刚学习了JavaMail技术,现在来应用到项目中试试~ 网站用户注册: 主要实现如下两个功能: 1.用户在网站上注册完成后给用户发一封邮件. 2.用户通过邮件激活后才可以登录. 思路: 首先需要一个思路: 用户在前台点击注册,向servlet提交请求,把用户提交过来的信息封装成一个JavaBean(需要的信息有name,pwd,email这3个是用户填写的,我们帮用户生成的

代码-java如何实现邮箱激活用户,和修改密码功能

问题描述 java如何实现邮箱激活用户,和修改密码功能 用户注册使用有代码向用户邮箱中发送链接,然后用户点击链接激活账号 解决方案 邮箱激活用户功能用Java实现邮箱激活账号功能用户注册后,通过网站发送到邮箱中的激活链接激活用户 java 解决方案二: 邮箱激活用户http://www.cxyclub.cn/n/60763/http://download.csdn.net/detail/martin_yl/8331813http://zfy421.iteye.com/blog/1691874 找

Yii框架实现邮箱激活的方法【数字签名】_php实例

本文实例讲述了Yii框架实现邮箱激活的方法.分享给大家供大家参考,具体如下: 控制器: //发送邮箱,激活账号 public function actionEmail() { $email=Yii::$app->request->get('email'); //数字签名 $em_1=md5($email); //邮箱发送 $mail= Yii::$app->mailer->compose(); $mail->setTo($email); $mail->setSubjec

Android账号注册实现点击获取验证码倒计时效果_Android

网站中为了防止恶意获取验证短信.验证邮箱,都会在点击获取验证码的按钮上做个倒计时的效果,如何实现这个效果,具体内容如下 效果图:   代码: RegisterActivity.java import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import com.jialianjia.bzw.BaseAct