忘记密码(通过邮箱来进⾏重新设置密码)忘记密码(通过邮箱来进⾏重新设置密码)
*总的流程
1.点击忘记密码
页⾯的代码
<!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>
2.进⼊到⾝份验证环节(⾝份校验)
⼀.页⾯的代码
<!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 Writer();
String uname = Parameter("uname");
System.out.println(uname);
String email = Parameter("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= JSONStringWithDateFormat(re,"yyyy-MM-dd");
out.print(json);
//验证⾝份成功之后发送邮件给⽤户验证码
String pt ="1596481760@qq";
String yzm22 = mail(email, pt).toString();
System.out.println("单个查询成功存下的验证码"+yzm22);
}else{
System.out.println("sb");
resultOne re=new resultOne();
re.setIstrue(2);
修改密码qqre.setMsg("该⽤户名邮箱下不存在账号");
String json= JSONStringWithDateFormat(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= Connection();
ps=con.prepareStatement(sql);
uteQuery();
()){
users=new sys_String("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
String host ="smtp.qq";//QQ 邮件服务器
// 获取系统属性
Properties properties = Properties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth","true");
/
/ 获取默认session对象
Session session = DefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
//发件⼈邮件⽤户名、授权码【打开邮箱-设置-⽣成授权码==password】(⾃⼰的邮箱和授权码)return new PasswordAuthentication("xxxxxxx@qq","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;
}
}