package main.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import main.DAO.DAO;
import main.Util.EmailUtil;
import main.Util.JDBCUtil;
import main.Util.User;
// Servlet of forgetPassword page
// Class "EmailServlet" is to send verification code to user and to check the code
public class EmailServlet extends HttpServlet {
private String user_email;
private String vCode; // Generated verification code
private String vCodeReceive; // Entered verification code
private PrintWriter out; // Output stream
private final EmailUtil emailUtil = EmailUtil.instance;
// Class "EmailServlet"'s construction method
public EmailServlet() {
System.out.println("Monitoring point: Email Servlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
out = response.getWriter();
user_email = request.getParameter("user_email"); // Parameter "email" from fore-end
vCodeReceive = request.getParameter("verification_code"); // Parameter "vcode" from fore-end
String method = request.getParameter("method"); // Parameter "method" is to choose what method is needed
switch (method) {
case "getVCode": // when "Get Code button" is pressed
try {
MGetVCode();
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case "verify": // when "Sign In button" is pressed
HttpSession session = request.getSession(); // Get session
session.setAttribute("email", user_email); // Transferring information to session
MVerify();
break;
default:
break;
}
out.flush();
out.close();
}
// Method "MGetVCode" is to confirm email address and send verification code
private void MGetVCode() throws Exception {
DAO dao = new DAO();
User user = new User(user_email, null);
JDBCUtil db = new JDBCUtil();
Connection connection = db.GetConn(); // Configure connection and other things
System.out.println("Monitoring point: Sending Verification Code");
if(!IsEmail(user_email)) { // Verify format of email address
out.print("-1"); // a label to make judgement of the condition of wrong format
return; // Interrupt process
} else {
if (!dao.Exist(connection, user)) { // Verify whether this email address is registered
out.print("0"); // a label to make judgement of the condition of nonexistent email address
return;
}
}
try {
emailUtil.SendEmail(user_email); // Send verification code to target email address
} catch (Exception e) {
e.printStackTrace();
}
vCode = emailUtil.getVCode(); // Get and store the verification code sending just now
out.print("1"); // a label to make judgement of the condition
}
// Method "IsEmail" is to provide a method to confirm the format of email address
private boolean IsEmail(String email) {
if(email.length() == 0) {
return false;
}
Pattern pattern = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"); // Regular expression
return pattern.matcher(email).matches();
}
// Method "MVerify" is to verify whether the verification code is correct
private void MVerify() {
System.out.println("Monitoring point: Verify Verification Code");
if(vCode.equals(vCodeReceive)) {
out.print("1"); // a label to make judgement of the condition
}
else {
out.print("-1"); // a label to make judgement of the condition
}
}
public void destroy() {
super.destroy();
}
@Override
public void init() throws ServletException {
super.init();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
(免积分获取资源:点开本人的主页,点击“查看详细资料”,添加好友获取源码文件(如果有问题同样可以通过这里问),本人承诺无特殊情况,三小时内将无条件提供源码(所谓特殊情况仅指时间上的,毕竟挂上去的是我的副号,获取信息可能不及时,见谅)) 关联文章:https://blog.csdn.net/weixin_61864411/article/details/128637695 JavaWeb登录注册系统/界面 有邮箱验证码; 通过MySQL数据库连接; 附有详细注释; 可作结课作业,可用于学习,可接入其他主系统。
资源推荐
资源详情
资源评论
收起资源包目录
WebProject.zip (61个子文件)
WebProject
lib
javax.mail-1.6.2.jar 644KB
mysql-connector-j-8.0.31.jar 2.4MB
fastjson-2.0.21.graal.jar 180KB
activation-1.1.jar 62KB
src
main
Util
VCodeUtil.java 1KB
EmailUtil.java 3KB
JDBCUtil.java 1KB
User.java 709B
DAO
DAO.java 3KB
Servlet
EmailServlet.java 5KB
LogoutServlet.java 2KB
SignServlet.java 4KB
out
artifacts
WebProject_war_exploded
CSS
styleMainPage.css 7KB
styleIndex.css 10KB
sendEmail.js 3KB
WEB-INF
classes
java
module2
module1
main
module2
Util
JDBCUtil.class 1KB
User.class 879B
VCodeUtil.class 904B
EmailUtil.class 3KB
DAO
DAO.class 3KB
module1
Servlet
EmailServlet.class 4KB
LogoutServlet.class 2KB
SignServlet.class 3KB
web.xml 1KB
index.jsp 4KB
mainPage.jsp 2KB
JSP
JS
styleForgetPassword.css 8KB
forgetPassword.jsp 3KB
jquery-1.11.3.js 278KB
web
styleMainPage.css 7KB
styleIndex.css 10KB
sendEmail.js 3KB
WEB-INF
classes
main
Util
JDBCUtil.class 1KB
User.class 879B
VCodeUtil.class 904B
EmailUtil.class 3KB
DAO
DAO.class 3KB
Servlet
EmailServlet.class 4KB
LogoutServlet.class 2KB
SignServlet.class 3KB
web.xml 1KB
index.jsp 4KB
mainPage.jsp 2KB
styleForgetPassword.css 8KB
forgetPassword.jsp 3KB
jquery-1.11.3.js 278KB
.idea
dataSources
dataSources.local.xml 468B
uiDesigner.xml 9KB
sqldialects.xml 214B
artifacts
WebProject_war_exploded.xml 515B
webContexts.xml 301B
workspace.xml 11KB
misc.xml 261B
dataSources.xml 530B
inspectionProfiles
Project_Default.xml 2KB
compiler.xml 236B
modules.xml 267B
.gitignore 184B
encodings.xml 378B
jsLibraryMappings.xml 202B
WebProject.iml 1KB
共 61 条
- 1
资源评论
山河之书Liu_Zixin
- 粉丝: 167
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功