页面的代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面title>
head>
<body>
<div class="reg_form">
<form action="userServlet?type=1" method="post">
<ul>
<li>
<label >用户名:label><input type="text" name="uname">
<span class=""><i class="">i>span>
li>
<li>
<label >密码:label><input type="password" name="pwd">
<span class=""><i class="">i>span>
li>
<li>
<a href="update.html">忘记密码a>
li>
<li>
<input class="btn" type="submit" value="登录">
li>
ul>
form>
div>
body>
html>
一.页面的代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>忘记密码title>
<script src="demo/js/jquery-3.6.0.js">script>
<script src="demo/js/jquery.min.js">script>
<script>
function sendEmail() {
alert(12);
var uname=$("#uname").val();
var email=$("#email").val();
$.ajax({
url:"userServlet?type=11",
type:"post",
dataType:"json",
data:{"uname":uname,"email":email},
success:function (data) {
alert(data.msg);
if (data.istrue==1){
location.href="inputYzm.html";
}
} ,
error:function (data) {
alert(data.msg);
if (data.istrue==2){
location.href="update.html";
}
}
});
}
script>
head>
<body>
<div class="reg_form">
<form >
<ul>
<li>
<label >账号:label><input type="text" id="uname">
<span class=""><i class="">i>span>
li>
<li>
<label >邮箱:label><input type="text" id="email">
<span class=""><i class="">i>span>
li>
<li>
<input class="btn" type="button" onclick="sendEmail()" value="确认">
li>
ul>
form>
div>
body>
html>
二.后台代码(servlet)
//单个查询+发送邮件 生成4位随机数字
protected void selectOne(HttpServletRequest req, HttpServletResponse resp) throws Exception {
PrintWriter out=resp.getWriter();
String uname = req.getParameter("uname");
System.out.println(uname);
String email = req.getParameter("email");
System.out.println(email);
sys_users one = ss.selectOne(new sys_users(uname,email));//测试数据
if(one!=null){
System.out.println("cg");
resultOne re=new resultOne();
re.setIstrue(1);
re.setMsg("该用户名邮箱下存在账号");
String json= JSON.toJSONStringWithDateFormat(re, "yyyy-MM-dd");
out.print(json);
//验证身份成功之后 发送邮件给用户 验证码
String pt = "[email protected]";
String yzm22 = emailTest.getyzemail(email, pt).toString();
req.getSession().setAttribute("yzm22",yzm22);
System.out.println("单个查询成功存下的验证码"+yzm22);
}else{
System.out.println("sb");
resultOne re=new resultOne();
re.setIstrue(2);
re.setMsg("该用户名邮箱下不存在账号");
String json= JSON.toJSONStringWithDateFormat(re, "yyyy-MM-dd");
out.print(json);
//验证身份失败
}
out.flush();
out.close();
}
三.后台代码(serviceImpl)
// 7: .单个查询 验证用户名 (写全单个查询 后面 还用的上)
@Override
public sys_users selectOne(sys_users u) {
//编写单个查询的 sql语句
String sql="select * from sys_users where uname='"+u.getUname()+"' and email='"+u.getEmail()+"'";
sys_users users = sd.selectOne(sql);
return users;
}
四.后台代码(daoImpl)
// 7: .单个查询 验证用户名 (写全单个查询 后面 还用的上)
@Override
public sys_users selectOne(String sql) {
sys_users users=null;
try {
con= jdbcUtil.getConnection();
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if (rs.next()){
users=new sys_users(rs.getString("uname"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
五.emailTest类中的 发送邮件的方法
public class emailTest {
public static Integer getyzemail(String to,String from) {
Integer random1 = 0;
// 收件人电子邮箱
// 发件人电子邮箱
// 指定发送邮件的主机为 smtp.qq.com
String host = "smtp.qq.com"; //QQ 邮件服务器
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
//发件人邮件用户名、授权码【打开邮箱-设置-生成授权码==password】(自己的邮箱和授权码)
return new PasswordAuthentication("[email protected]", "uuekxrwqqyeffjde");
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("通知");
//产生随机数
random1 = (int) (Math.random()*(9999-1000+1))+1000;
// 设置消息体
message.setText("验证码是"+random1);
// 发送消息
Transport.send(message);
System.out.println("发送成功");
}catch (MessagingException mex) {
mex.printStackTrace();
}
return random1;
}
}
输入验证码验证的页面
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title>
<script src="demo/js/jquery-3.6.0.js">script>
<script src="demo/js/jquery.min.js">script>
<script>
function yzEmailyzm() {
alert(22);
var email=$("#email").val();
$.ajax({
url:"userServlet?type=10",
type:"post",
dataType:"json",
data:{"email":email},
success:function (data) {
alert(data.msg);
if (data.istrue==1){
location.href="hello.jsp";
}
} ,
error:function (data) {
}
});
}
script>
head>
<body>
<h1>请输入验证码h1>
<div class="reg_form">
<form >
<ul>
<li>
<label >验证码:label><input type="text" id="email">
<span class=""><i class="">i>span>
li>
<li>
<input class="btn" type="button" onclick="yzEmailyzm()" value="确认验证码">
li>
ul>
form>
div>
body>
html>
输入验证码验证的后台
protected void yzemailyzm(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//自己输入验证码
String yzm = req.getParameter("email");
//自动生成的验证码
String yzm2 = (String) req.getSession().getAttribute("yzm22");
System.out.print(yzm + "==============" + yzm2);
PrintWriter out = resp.getWriter();
resultOne re = new resultOne();
if (yzm.equals(yzm2)) {
System.out.print("验证成功");
//验证码验证成功 到修改页面 修改密码
re.setIstrue(1);
re.setMsg("验证码验证成功");
String json = JSON.toJSONStringWithDateFormat(re, "yyyy-MM-dd");
out.println(json);
} else {
System.out.print("验证失败");
}
out.flush();
out.close();
}
验证跳转到修改用户名的页面》》》》》》》
进入页面后加载出单个查询的 账号和邮箱显示在页面上
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改密码 页面title>
<script src="demo/js/jquery-3.6.0.js">script>
<script src="demo/js/jquery.min.js">script>
<script>
function selectInfo() {
//通过ajax去后台查询数据
$.ajax({
url:"userServlet?type=14",
dataType:"json",
type:"post",
// data:{},
success:function (data) {
$("#uname").val(data.uname);
$("#email").val(data.email);
} ,
error:function (data) {
}
});
}
script>
head>
<body onload="selectInfo()">
<h1>修改密码页面h1>
<div class="reg_form">
<form>
<ul>
<li>
<label>账号:label><input type="text" id="uname">
<span class=""><i class="">i>span>
li>
<li>
<label>邮箱:label><input type="text" id="email">
<span class=""><i class="">i>span>
li>
<li>
<label>密码:label><input type="text" id="pwd">
<span class=""><i class="">i>span>
li>
<li>
<input class="btn" type="button" onclick="updatePwd()" value="确认">
li>
ul>
form>
div>
body>
html>
显示在页面上的 方法
protected void one_infoName(HttpServletRequest req, HttpServletResponse resp) throws Exception {
PrintWriter out=resp.getWriter();
resultOne re=new resultOne();
String selectone_uname = (String)req.getSession().getAttribute("selectone_uname");
System.out.println(selectone_uname);
String selectone_email = (String)req.getSession().getAttribute("selectone_email");
System.out.println(selectone_email);
re.setUname(selectone_uname);
re.setEmail(selectone_email);
String json = JSON.toJSONStringWithDateFormat(re, "yyyy-MM-dd");
out.println(json);
out.flush();
out.close();
}