案例14-实现注册功能和激活功能

1WebContent部分

1 register.jsp代码修改

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head></head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员注册</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />

<style>
body {
    margin-top: 20px;
    margin: 0 auto;
}

.carousel-inner .item img {
    width: 100%;
    height: 300px;
}

font {
    color: #3164af;
    font-size: 18px;
    font-weight: normal;
    padding: 0 10px;
}
</style>
<script type="text/javascript">
    /* 实现验证码点击改变效果 */
    function changeImge(obj){
        obj.src="${pageContext.request.contextPath }/checkImg?time="+new Date().getTime();
    }
</script>


</head>
<body>

    <!-- 引入header.jsp -->
    <jsp:include page="/header.jsp"></jsp:include>

    <div class="container"
        style="width: 100%; background: url('image/regist_bg.jpg');">
        <div class="row">
            <div class="col-md-2"></div>
            <div class="col-md-8"
                style="background: #fff; padding: 40px 80px; margin: 30px; border: 7px solid #ccc;">
                <font>会员注册</font>USER REGISTER
                <form class="form-horizontal"  action="${pageContext.request.contextPath }/register" method="post" style="margin-top: 5px;">
                    <div class="form-group">
                        <label for="username" class="col-sm-2 control-label">用户名</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="username" name="username"
                                placeholder="请输入用户名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" id="inputPassword3" name="password"
                                placeholder="请输入密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="confirmpwd" class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" id="confirmpwd"
                                placeholder="请输入确认密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
                        <div class="col-sm-6">
                            <input type="email" class="form-control" id="inputEmail3" name="email"
                                placeholder="Email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="usercaption" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="usercaption" name="name"
                                placeholder="请输入姓名">
                        </div>
                    </div>
                    <div class="form-group opt">
                        <label for="inlineRadio1" class="col-sm-2 control-label">性别</label>
                        <div class="col-sm-6">
                            <label class="radio-inline"> <input type="radio"
                                name="sex" id="sex1" value="male"></label> <label class="radio-inline"> <input type="radio"
                                name="sex" id="sex2" value="female"></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="date" class="col-sm-2 control-label">出生日期</label>
                        <div class="col-sm-6">
                            <input type="date" class="form-control" name="birthday"> 
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="date" class="col-sm-2 control-label">验证码</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="checkCode">
                        </div>
                        <div class="col-sm-2">
                            <img src="${pageContext.request.contextPath }/checkImg" onclick="changeImge(this)"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <input type="submit" width="100" value="注册" name="submit"
                                style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;">
                            <div>${registInfo }</div>
                        </div>
                    </div>
                </form>
            </div>

            <div class="col-md-2"></div>

        </div>
    </div>

    <!-- 引入footer.jsp -->
    <jsp:include page="/footer.jsp"></jsp:include>

</body>
</html>

2 registerFail.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>对不起,注册失败,请联系管理员 010-68888888</h2>
</body>
</html>

 

3 registerSuccess.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>恭喜您注册成功,请赶快前往注册邮箱激活您的帐户</h2>
    <div>
        <span><a href="http://mail.163.com/">网易163邮箱</a></span>&nbsp;&nbsp;&nbsp;
        <span><a href="http://mail.126.com/">网易126邮箱</a></span>&nbsp;&nbsp;&nbsp;
        <span><a href="https://mail.qq.com/cgi-bin/loginpage">腾讯QQ邮箱</a></span>&nbsp;&nbsp;&nbsp;
    </div>
</body>
</html>

 

2 web层

1RegisterServlet代码

注意自动映射封装的时候,如何进行类型的转换。如String 转换成date类型。本例中获取到的birthday需要转成date类型。

 

package www.test.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import www.test.domain.User;
import www.test.service.UserService;
import www.test.utils.CommonsUtils;
import www.test.utils.MailUtils;

public class RegisterServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 解决乱码问题
        request.setCharacterEncoding("UTF-8");

        // 判断验证码是否正确
        // 获得页面输入的验证
        String checkCode_client = request.getParameter("checkCode");
        // 获得生成图片的文字的验证码
        String checkCode_session = (String) request.getSession().getAttribute("checkcode_session");
        // 比对页面的和生成图片的文字的验证码是否一致
        if (!checkCode_session.equals(checkCode_client)) {
            request.setAttribute("registInfo", "您的验证码不正确");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            return; // 验证码输入错误的话,就没有必要获取输入的用户名和密码等其他信息
        }

        // 获得表单数据
        Map<String, String[]> properties = request.getParameterMap();
        User user = new User();
        try {
            // 自己指定一个类型转换器(将String转成Date)
            // ConvertUtils.register(converter, clazz);
            // converter : 转换器接口 clazz:要转换成的类型
            ConvertUtils.register(new Converter() {

                @Override
                // value要转换的值
                public Object convert(Class clazz, Object value) {
                    // 将string转成date
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = null;
                    try {
                        date = dateFormat.parse(value.toString());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    return date;
                }
            }, Date.class);

            // 进行映射封装
            BeanUtils.populate(user, properties);
        } catch (IllegalAccessException | InvocationTargetException e) {

            e.printStackTrace();
        }

        // 到这里user就封装了
        // 下面手动封装剩余的项
        // private String uid;
        user.setUid(CommonsUtils.getUUID());
        // private String telephone;
        user.setTelephone(null);
        // private int state;//是否激活
        user.setState(0);
        // private String code;//激活码
        String activeCode = CommonsUtils.getUUID();
        user.setCode(activeCode);

        // 将user传递给service层
        UserService service = new UserService();
        boolean isRegisterSuccess =true;
        try {
            isRegisterSuccess = service.regist(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        //是否注册成功
        if(isRegisterSuccess){
            //发送激活邮件
            String emailMsg = "恭喜您注册成功,请点击下面的连接进行激活账户"
                    +"<a href='http://localhost:8080/WEBTestShop28/active?activeCode="+activeCode+"'>"
                            + "http://localhost:8080/WEBTestShop28/active?activeCode="+activeCode+"</a>";
            String subject = "激活邮件";
            
            try {
                MailUtils.sendMail(user.getEmail(), subject, emailMsg);
            } catch (MessagingException e) {
                
                e.printStackTrace();
            }
            
            //跳转到注册成功页面
            response.sendRedirect(request.getContextPath()+"/registerSuccess.jsp");
        }else{
            //跳转到失败的提示页面
            response.sendRedirect(request.getContextPath()+"/registerFail.jsp");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

2 ActiveServlet代码

package www.test.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.test.service.UserService;

public class ActiveServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获得激活码
        String activeCode = request.getParameter("activeCode");
        
        //将获取到的激活码传递给service层
        UserService service = new UserService();
        boolean isActiveSuccess = true;
        try {
            isActiveSuccess = service.active(activeCode);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        
        if(isActiveSuccess){
            //激活成功跳转到登录页面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{ //激活失败跳转到注册页面
            response.sendRedirect(request.getContextPath()+"/register.jsp");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

3 CheckImgServlet代码

package www.test.web.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 验证码生成程序
 */
public class CheckImgServlet extends HttpServlet {

    // 集合中保存所有成语
    private List<String> words = new ArrayList<String>();

    @Override
    public void init() throws ServletException {
        // 初始化阶段,读取new_words.txt
        // web工程中读取文件,必须使用绝对磁盘路径
        String path = getServletContext().getRealPath("/WEB-INF/new_words.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            String line;
            while ((line = reader.readLine()) != null) {
                words.add(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 禁止缓存
        // response.setHeader("Cache-Control", "no-cache");
        // response.setHeader("Pragma", "no-cache");
        // response.setDateHeader("Expires", -1);

        int width = 120;
        int height = 30;

        // 步骤一 绘制一张内存中图片
        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);

        // 步骤二 图片绘制背景颜色 ---通过绘图对象
        Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
        // 绘制任何图形之前 都必须指定一个颜色
        graphics.setColor(getRandColor(200, 250));
        graphics.fillRect(0, 0, width, height);

        // 步骤三 绘制边框
        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, 0, width - 1, height - 1);

        // 步骤四 四个随机数字
        Graphics2D graphics2d = (Graphics2D) graphics;
        // 设置输出字体
        graphics2d.setFont(new Font("宋体", Font.BOLD, 18));

        Random random = new Random();// 生成随机数
        int index = random.nextInt(words.size());
        String word = words.get(index);// 获得成语

        // 定义x坐标
        int x = 10;
        for (int i = 0; i < word.length(); i++) {
            // 随机颜色
            graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random
                    .nextInt(110), 20 + random.nextInt(110)));
            // 旋转 -30 --- 30度
            int jiaodu = random.nextInt(60) - 30;
            // 换算弧度
            double theta = jiaodu * Math.PI / 180;

            // 获得字母数字
            char c = word.charAt(i);

            // 将c 输出到图片
            graphics2d.rotate(theta, x, 20);
            graphics2d.drawString(String.valueOf(c), x, 20);
            graphics2d.rotate(-theta, x, 20);
            x += 30;
        }
        // 将验证码内容保存session
        request.getSession().setAttribute("checkcode_session", word);

        // 步骤五 绘制干扰线
        graphics.setColor(getRandColor(160, 200));
        int x1;
        int x2;
        int y1;
        int y2;
        for (int i = 0; i < 30; i++) {
            x1 = random.nextInt(width);
            x2 = random.nextInt(12);
            y1 = random.nextInt(height);
            y2 = random.nextInt(12);
            graphics.drawLine(x1, y1, x1 + x2, x2 + y2);
        }

        // 将上面图片输出到浏览器 ImageIO
        graphics.dispose();// 释放资源
        
        //将图片写到response.getOutputStream()中
        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * 取其某一范围的color
     * 
     * @param fc
     *            int 范围参数1
     * @param bc
     *            int 范围参数2
     * @return Color
     */
    private Color getRandColor(int fc, int bc) {
        // 取其随机颜色
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

}

 

3 service层

package www.test.service;

import java.sql.SQLException;

import www.test.dao.UserDao;
import www.test.domain.User;

public class UserService {
    //注冊
    public boolean regist(User user) throws SQLException {
        UserDao dao = new UserDao();
        int row = dao.regist(user);
        return row>0?true:false;
    }

    //激活
    public boolean active(String activeCode) throws SQLException {
        UserDao dao = new UserDao();
        int row = dao.active(activeCode);
        return row>0?true:false;
    }

}

4 dao层代码

package www.test.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import www.test.domain.User;
import www.test.utils.C3P0Utils;

public class UserDao {

    //注冊
    public int regist(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql ="insert into user values (?,?,?,?,?,?,?,?,?,?);";
        int row = qr.update(sql, user.getUid(),user.getUsername(),user.getPassword(),
                user.getName(),user.getEmail(),user.getTelephone(),
                user.getBirthday(),user.getSex(),user.getState(),user.getCode());
        return row;
    }

    //激活
    public int active(String activeCode) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql ="update user set state=? where code=?";
        int row = qr.update(sql, 1,activeCode);
        return row;
    }

}

5 utils部分

1 CommonsUtils代码

package www.test.utils;

import java.util.UUID;

public class CommonsUtils {

    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}

2 MD5Utils代码

package www.test.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    /**
     * 使用md5的算法进行加密
     */
    public static String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法!");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
        // 如果生成数字未满32位,需要前面补0
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }

    public static void main(String[] args) {
        System.out.println(md5("123"));
    }

}

3 MailUtils代码

package www.test.utils;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
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 javax.mail.internet.MimeMessage.RecipientType;

import com.sun.mail.util.MailSSLSocketFactory;

public class MailUtils {

    //email:邮件发给谁  subject:主题  emailMsg:邮件的内容
    public static void sendMail(String email, String subject, String emailMsg)
            throws AddressException, MessagingException {

        // 1.创建一个程序与邮件服务器会话对象 Session
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "SMTP");//发邮件的协议
        props.setProperty("mail.host", "smtp.126.com");//发送邮件的服务器地址
        props.setProperty("mail.smtp.auth", "true");// 指定验证为true

        //开启SSL加密  ,QQ邮箱必须开启ssl加密才可以。
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        } catch (GeneralSecurityException e) {

            e.printStackTrace();
        }
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("luojun281", "123456ljp");//发邮件的账号的验证 12345为授权码,并非密码
            }
        };

        //这个是邮箱服务器的session 和之前学的session不是同一个session
        Session session = Session.getInstance(props, auth);

        // 2.创建一个Message,它相当于是邮件内容
        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress("luojun281@126.com")); // 设置发送者

        message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者

        message.setSubject(subject);//邮件的主题

        message.setContent(emailMsg, "text/html;charset=utf-8");

        // 3.创建 Transport用于将邮件发送
        Transport.send(message);
    }
}

4 c3p0utils代码

package www.test.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {

    // 1 获得Connection ----- 从连接池中获取
    private static DataSource dataSource = new ComboPooledDataSource();

    // 2 创建ThreadLocal 存储的类型是Connection
    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    // 3 直接可以获取一个连接池
    public static DataSource getDataSource() {
        return dataSource;
    }

    // 4 直接获取一个连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    // 5 获取绑定到ThreadLocal上的连接对象
    public static Connection getCurrentConnection() throws SQLException {
        //从ThreadLocal寻找 当前线程是否有对应Connection
        Connection con = tl.get();
        if (con == null) {
            //获得新的connection
            con = dataSource.getConnection();
            //将conn资源绑定到ThreadLocal(map)上
            tl.set(con);
        }
        return con;
    }

    // 6 开启事务
    public static void startTransaction() throws SQLException {
        Connection con = getCurrentConnection();
        if (con != null) {
            con.setAutoCommit(false);
        }
    }

    // 7 事务回滚
    public static void rollback() throws SQLException {
        Connection con = getCurrentConnection();
        if (con != null) {
            con.rollback();
        }
    }

    //  8 提交并且 关闭资源及从ThreadLocall中释放
    public static void commitAndRelease() throws SQLException {
        Connection con = getCurrentConnection();
        if (con != null) {
            con.commit(); // 事务提交
            con.close();// 关闭资源
            tl.remove();// 从线程绑定中移除
        }
    }

    // 9 关闭资源方法
    public static void close(ResultSet rs, Statement st, Connection con) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (st != null) {
            st.close();
        }
        if (con != null) {
            con.close();
        }
    }
}

6 domain部分

1 user类

package www.test.domain;

import java.util.Date;

public class User {

     /*`uid` varchar(32) NOT NULL,
      `username` varchar(20) DEFAULT NULL,
      `password` varchar(20) DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL,
      `email` varchar(30) DEFAULT NULL,
      `telephone` varchar(20) DEFAULT NULL,
      `birthday` date DEFAULT NULL,
      `sex` varchar(10) DEFAULT NULL,
      `state` int(11) DEFAULT NULL,
      `code` varchar(64) DEFAULT NULL,*/
    
    private String uid;
    private String username;
    private String password;
    private String name;
    private String email;
    private String telephone;
    private Date birthday;
    private String sex;
    private int state;//是否激活
    private String code;//激活码
    
    public String getUid() {
        return uid;
    }
    public void setUid(String uid) {
        this.uid = uid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

 

posted @ 2018-02-11 14:15  Jepson6669  阅读(456)  评论(0编辑  收藏  举报