2.14 登录页面

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 java.io.IOException;

@WebServlet(value = "/updatepwd_servlet")
public class UpdatePwdServlet extends HttpServlet {
    private String code;
    private String password;
    private Base_InformationBean hhh;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        code=request.getParameter("nameid");
        password=request.getParameter("pwd");
        hhh=new Base_InformationBean();
        hhh.setCode(code);
        hhh.setPassword(password);
        boolean flag= new InfoDAO().updateCode(hhh);
        request.setAttribute("msg","密码修改完成");
        try {
            request.getRequestDispatcher("index.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);
    }
}
复制代码

登录后界面

复制代码
<%@ 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>
复制代码

修改密码的确认密码:

复制代码
<%--
  Created by IntelliJ IDEA.
  User: 龚涵彬
  Date: 2024/2/4
  Time: 16:47
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
复制代码
复制代码
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);
    }
}
复制代码
posted @ 2024-02-28 14:09  new菜鸟  阅读(13)  评论(0)    收藏  举报