2024/02/10

我们在登录之后账号已经存入session中,所以我们要修改密码的流程中就需要验证原密码先。

登录后页面:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div style="text-align: center">
    <h1 style="text-align: center;color: red">您已经登录成功</h1>
    <jsp:useBean id="Info" class="bean.Base_InformationBean">
        <jsp:setProperty name="Info" property="code" value="${sessionScope.User.code}"></jsp:setProperty>
        <jsp:setProperty name="Info" property="password" value="密码不能告诉你哟"></jsp:setProperty>
    </jsp:useBean>
    <p>
        <jsp:getProperty name="Info" property="code"/>
    </p>
    <p>
        <jsp:getProperty name="Info" property="password"/>
    </p>
    <a href="confirm.jsp">修改密码</a>
</div>
</body>
</html>

然后是验证密码界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>身份验证</h1>
<div style="text-align: center">
    <label>您的账号</label><input name="id" readonly value="${sessionScope.User.code}" >
    <form id="change_pwd" action="confirm-servlet">
        <label for="pwd">请输入旧密码</label><input name="pwd" id="pwd" type="text"><br>
        <span id="msg" style="font-size: 12px;color:red">${msg}</span><br><br>
        <button type="button" id="btm">确认</button>
    </form>
    <script>
        document.getElementById("btm").addEventListener("click",function () {
            var pwd=document.getElementById("pwd").value;
            if(pwd.trim()===""||pwd.length===0)
            {
                document.getElementById("msg").innerText="密码不能为空";
                return;
            }
            document.getElementById("change_pwd").submit();
        })
    </script>
</div>
</body>
</html>

验证的servlet,我们在验证通过后就需要将我们之前存入的session给销毁,然后通过普通的

request.setAttribute("code",code);
            try {
                request.getRequestDispatcher("change_pwd.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }

结构来携带我们的账号跳转到输入新密码的界面。

package com.example.demo;

import bean.Base_InformationBean;
import bean.InfoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value="/confirm-servlet")
public class ConfirmServlet extends HttpServlet {
    private String code;
    private String password;
    private Base_InformationBean hhh;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session= request.getSession();
        code=((Base_InformationBean)session.getAttribute("User")).getCode();
        System.out.println(code);
        password=request.getParameter("pwd");
        hhh=new Base_InformationBean();
        hhh.setCode(code);
        hhh.setPassword(password);
        boolean flag= new InfoDAO().Login(hhh);
        if(flag)
        {
            request.setAttribute("code",code);
            try {
                request.getRequestDispatcher("change_pwd.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
            session.removeAttribute("User");
            //手动注销
            session.invalidate();
        }
        else
        {
            request.setAttribute("msg","密码错误");
            try {
                request.getRequestDispatcher("confirm.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        doGet(request,response);
    }
}

这样就实现了输入新密码界面的复用。

这里要知道button的type有三个属性submit会直接提交,button是可点击事件,reset是重置表单数据,我们设置script是要使用的是button属性

script中的innerText是设置本标签的文本内容而innerHTML可以修改HTML内容和属性

posted @ 2024-02-10 20:47  伐木工熊大  阅读(12)  评论(0编辑  收藏  举报