JavaWeb网上图书商城完整项目--day02-8.提交注册表单功能之dao、service实现
1、发送邮件

发送邮件的时候的参数我们都写在了配置文件中,配置文件放在src目录下,可以使用类加载器进行加载该数据
//向注册的用户发送邮件
//1读取配置文件
Properties properties = new Properties();
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
} catch (IOException e1) {
throw new RuntimeException(e.getMessage());
}
<a href\="http\://localhost\:8080/goods/UserServlet?method\=activation&activationCode\={0}",这里有一个占位符{0},第二个占位符对应应该是{1},我们可以使用实际的值替换对应的占位符。
这里我们定义了两个占位符,其中的数字对应于传入的参数数组中的索引,{0}占位符被第一个参数替换,{1}占位符被第二个参数替换,依此类推。
String content = properties.getProperty("content");
//替换占位符
MessageFormat.format(content, user.getActivationCode());//替换占位符

收到邮件之后点击href,交给UserServlet的activation方法进行处理,并且把activationCode激活码携带过来。此激活码会和保存到数据库中的激活码进行比较
我们来看UserServlet的代码:
package com.weiyuan.goods.user.web.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.weiyuan.goods.user.service.UserService; import cn.itcast.servlet.BaseServlet; /**
* Servlet implementation class UserServlet
*/
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet{
private static final long serialVersionUID = 1L;
private UserService service = new UserService();
/*
* 用户注册页面使用ajax校验/*
* 用户注册页面使用ajax校验用户名会调用该方法
* *会调用该方法
* */
public String validateLoginname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//首先获得用户上传的用户名
String loginName = request.getParameter("loginname");
boolean flag = service.ajaxValidateLoginName(loginName);
response.getWriter().print(flag);
return null;
}
/*
* 用户注册页面使用ajax校验邮箱会调用该方法
* */
public String validateEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得用户上传的emai String email = request.getParameter("email");
System.out.println("validateEmail is called"+email);
boolean flag = service.ajaxValidateEmail(email);
response.getWriter().print(flag);
return null;
} /*
* 用户注册页面使用ajax校验验证码会调用该方法
* */
public String validateVerifyCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得用户上传的verfycode
String verifyCode = request.getParameter("verifyCode");
//获得session中保存的验证码
String sessionCode = (String) request.getSession().getAttribute("vCode");
//二者进行比较看是否相等
System.out.println("validateVerifyCode is called"+verifyCode+":"+sessionCode);
boolean flag = sessionCode.equalsIgnoreCase(verifyCode);
response.getWriter().print(flag);
return null;
} /*
* 当用户注册的时候会调用该方法
*
* */
public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("registis called");
return null;
} /*
* 当用户点击邮件的时候会调用该方法,会把邮件携带的激活码传递过来
*
* */ }
我们来看看业务层的代码:
package com.weiyuan.goods.user.service; import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties; import javax.mail.MessagingException;
import javax.mail.Session;
import javax.management.RuntimeErrorException; import cn.itcast.commons.CommonUtils;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils; import com.weiyuan.goods.user.dao.UserDao;
import com.weiyuan.goods.user.domian.User; public class UserService { private UserDao dao = new UserDao(); public boolean ajaxValidateLoginName(String loginName) { try {
return dao.ajaxValidateLoginName(loginName);
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e.getMessage());
} } public boolean ajaxValidateEmail(String email) { try {
return dao.ajaxValidateEmail(email);
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e.getMessage());
} } //添加注册的用户
public void addUser(User user){
//添加用户的uuid
user.setUid(CommonUtils.uuid());
//添加用户的激活码
String activationCode = CommonUtils.uuid()+CommonUtils.uuid();
user.setActivationCode(activationCode);
//当前处于未激活的状态
user.setStatus(0);//0表示未激活 try {
dao.addUser(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e.getMessage());
} //向注册的用户发送邮件
//1读取配置文件
Properties properties = new Properties();
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
} catch (IOException e1) {
throw new RuntimeException(e1.getMessage());
} String host = properties.getProperty("host"); //qq邮箱发送邮件的地址,端口465或者587
//qq接受邮件服务器的地址是pop.qq.com,端口995
String username=properties.getProperty("username"); //登陆服务器的账号
String password=properties.getProperty("password");//这里不是客户端登陆的密码,而是授权密码一定要注意
Session session = MailUtils.createSession(host, username, password);
//发送邮件
String from = properties.getProperty("from");//发件人
String to = user.getEmail();//收件人
String title = properties.getProperty("subject");
String content = properties.getProperty("content");
//替换占位符
MessageFormat.format(content, user.getActivationCode());//替换占位符
Mail mail = new Mail(from,to,title,content);
try {
MailUtils.send(session, mail);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} } }
我们来看看数据库的代码:
package com.weiyuan.goods.user.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.weiyuan.goods.user.domian.User;
import cn.itcast.jdbc.TxQueryRunner;
public class UserDao {
//操作数据库
private TxQueryRunner qr = new TxQueryRunner();
/***
* 查询用户名是否存在
* @throws SQLException
*/
public boolean ajaxValidateLoginName(String loginName) throws SQLException{
//获得满足记录的数目是对象,返回一个整数,整数是单行单列使用ScalarHandler
String sql ="select count(*) from t_user where loginname=?";
Number num = (Number) qr.query(sql, new ScalarHandler(),loginName);
int count = num.intValue();
if(count>0){
return true;
}
return false;
}
/***
* 查询邮箱是否存在
* @throws SQLException
*/
public boolean ajaxValidateEmail(String email) throws SQLException{
//获得满足记录的数目是对象,返回一个整数,整数是单行单列使用ScalarHandler
String sql ="select count(*) from t_user where email=?";
Number num = (Number) qr.query(sql, new ScalarHandler(),email);
int count = num.intValue();
System.out.println("count="+count);
if(count>0){
return true;
}
return false;
}
/***
* 添加注册的用户
* @throws SQLException
*/
public void addUser(User user) throws SQLException{
//获得满足记录的数目是对象,返回一个整数,整数是单行单列使用ScalarHandler
String sql ="insert into t_user values (?,?,?,?,?,?)";
Object[] params = {user.getUid(),user.getLoginname(),user.getLoginpass(),
user.getEmail(),user.getStatus(),user.getActivationCode()};
qr.update(sql, params);
}
}
JavaWeb网上图书商城完整项目--day02-8.提交注册表单功能之dao、service实现的更多相关文章
- JavaWeb网上图书商城完整项目--day02-27.查询所有分类功能之Servlet和Service层
我们在上面实现了数据库层的代码,现在我们来实现业务层和Servlet层的代码:业务层的代码如下: package com.weiyuan.goods.category.service; import ...
- JavaWeb网上图书商城完整项目--day02-4.regist页面提交表单时对所有输入框进行校验
1.现在我们要将table表中的输入的参数全部提交到后台进行校验,我们提交我们是按照表单的形式提交,所以我们首先需要在table表外面添加一个表单 <%@ page language=" ...
- JavaWeb网上图书商城完整项目--24.注册页面的css样式实现
现在框架已经做好了,即下来我们要对页面进行装饰了,第一步给每一个元素添加id 1.最外面的div添加id为divMain 2.第二个div添加id为divTitle,里面的span对应的id为span ...
- JavaWeb网上图书商城完整项目--过滤器解决中文乱码
我们知道,如果是POST请求,我们需要调用request.setCharacterEncoding(“utf-8”)方法来设计编码:如果是GET请求,我们需要自己手动来处理编码问题.如果我们使用了En ...
- JavaWeb网上图书商城完整项目--13.项目所需环境的搭建
1.首先安装mysql 创建项目所需的数据库,直接运行项目提供的goods.sql文库 2.myeclipse创建一个web project ,项目的名称是goods 把视频中提供的项目原型下的提供的 ...
- JavaWeb网上图书商城完整项目--BaseServlet
1.以前进行操作的时候,例如我们进行登陆操作我们使用LoginServlet进行处理,进行注册操作我们使用RegisterServlet,很多业务的操作的时候我们就要定义很多个Servlet 有了Ba ...
- JavaWeb网上图书商城完整项目--day02-21.退出功能的实现
1.当用户点击退出的时候,跳转到登陆页面 当用户点击退出的时候,需要将session中保存的登陆的用户销毁掉 当用户点击退出的时候,调用UserServlet的quit方法 退出按钮在top.jsp中 ...
- JavaWeb网上图书商城完整项目--day02-17.登录功能页面实现
1.当在登陆页面点击登陆按钮的时候,会调用UserServlet的login方法,我们要在login.jsp中进行配置 2.要在login.jsp中处理Servlet在后台业务操作之后forward到 ...
- JavaWeb网上图书商城完整项目--day03-1.图书模块功能介绍及相关类创建
1 前两天我们学习了user用户模块和图书的分类模块,接下来我们学习图书模块 图书模块的功能主要是下面的功能: 2 接下来我们创建对应的包 我们来看看对应的数据库表t_book CREATE TABL ...
- JavaWeb网上图书商城完整项目--day02-28.查询所有分类功能之left页面使用Q6MenuBar组件显示手风琴式下拉菜单
首先页面去加载的时候,会去加载main.js文件,我们在加载left.jsp.top.jsp body.jsp,现在我们修改main.jsp的代码,让它去请求的时候去访问的是不在直接去访问left.j ...
随机推荐
- GNS3内网通过cloud与实际网络实现互连互通的实验(使用环回网口)
一.背景: 在GNS3内构建一个测试网络,该测试网络的设备能够通过cloud访问外部网络设备和Internet网,外部网络也能直接访问GNS3内网的设备. 考虑通过cloud上的环回口连接GNS3内网 ...
- 当微信小程序遇到云开发,再加上一个类似 ColorUI 的模板,人人都能做小程序了
作为一个 Java 程序员,早就想尝试一把微信小程序,但是一直苦于没有想法,再加上做一个漂亮的页面实在不太擅长. 由于自己比较喜欢历史,经常看历史方面的书.在一次梳理中国现有的朝代时,突然想到,要是可 ...
- Rocket - tilelink - Filter
https://mp.weixin.qq.com/s/6XX0CZHoDotIgLbNDSIUog 简单介绍Filter的实现. 1. 基本介绍 使用过滤器过滤掉client和m ...
- jchdl - RTL实例 - Adder4Carry
https://mp.weixin.qq.com/s/j4zLmjKgau2vRXVNfm0SIA 带进位的加法. 参考链接 https://github.com/wjcdx/jchdl/bl ...
- eclipse中的Invalid text string (xxx).
这个是说明在eclipse中引用HTML的时候,语法出现了不规范的错误 可以到https://www.w3school.com.cn/index.html里面找找对应对象的问题 我之前就是option ...
- Java实现 蓝桥杯VIP 算法训练 步与血(递推 || DFS)
试题 算法训练 步与血 问题描述 有n*n的方格,其中有m个障碍,第i个障碍会消耗你p[i]点血.初始你有C点血,你需要从(1,1)到(n,n),并保证血量大于0,求最小步数. 输入格式 第一行3个整 ...
- Java实现 蓝桥杯VIP 算法训练 打印下述图形
算法训练 4-1打印下述图形 时间限制:1.0s 内存限制:256.0MB 问题描述 使用循环结构打印下述图形,打印行数n由用户输入.打印空格时使用"%s"格式,向printf函数 ...
- Java实现 蓝桥杯VIP 算法提高 师座操作系统
算法提高 师座操作系统 时间限制:1.0s 内存限制:256.0MB 问题描述 师座这天在程序设计课上学了指针和结构体以后,觉得自己可以轻松的写出操作系统,为了打败大微软帝国,他给这个系统起了个响亮的 ...
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
- java实现BellmanFord算法
1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶点之间的最短距离,其显著特点是可以求取含负权图的单源最短 ...