2024/02/02

修改密码:

<%--
  Created by IntelliJ IDEA.
  User: 龚涵彬
  Date: 2024/2/3
  Time: 16:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新密码</title>
</head>
<body>
<div style="text-align: center">
<form action="updatepwd_servlet" method="post" id="change_pwd">
    账号:<input type="text" value="${code}" name="nameid" readonly><br>
    <label for="pwd1">请输入新密码:</label><input type="password" name="pwd" id="pwd1"><br>
    <label for="pwd1">再次确认密码:</label><input type="password" name="p" id="pwd2"><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 pwd1=document.getElementById("pwd1").value;
            var pwd2=document.getElementById("pwd2").value;
            if(!is(pwd1))
            {
                document.getElementById('msg').innerHTML="您的新密码不能为空";
                return;
            }
            if(!is(pwd2))
            {
                document.getElementById('msg').innerHTML="您再次确认密码不能为空";
                return;
            }
            if(pwd1.length>20)
            {
                document.getElementById('msg').innerHTML="您的新密码长度超过限制";
                return;
            }
            if(pwd1!==pwd2)
            {
                document.getElementById('msg').innerHTML="您的两次密码输入不一致";
                return;
            }
            document.getElementById("change_pwd").submit();
        })
        function is(s)
        {
            if (s.length===0||s.trim()==="")
                return false;
            else return true;
        }
    </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 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);
    }
}

Javabean

package bean;

public class Base_InformationBean {
    private String code;
    private String password;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

数据库操作:

package bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil //用于连接数据库
{
    String name="root";
    String password="123456";
    public Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection("jdbc:mysql://localhost:3306/test",name,password);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public void closeConnection(Connection conn) {

        if(conn!=null)
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}
package bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InfoDAO {
    public boolean Login(Base_InformationBean baseInformationBean) //登录
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from hhh where code=? and password=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getCode());
            pstm.setString(2,baseInformationBean.getPassword());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public boolean updateCode(Base_InformationBean baseInformationBean) //修改密码
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="update hhh set password=? where code=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getPassword());
            pstm.setString(2,baseInformationBean.getCode());
            pstm.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public boolean findCode(Base_InformationBean baseInformationBean) //查找账号
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from hhh where code=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getCode());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public void register(Base_InformationBean baseInformationBean)//注册
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="insert into hhh(code,password) values(?,?)";
        try
        {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getCode());
            pstm.setString(2,baseInformationBean.getPassword());
            pstm.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

 

posted @ 2024-02-03 19:26  伐木工熊大  阅读(2)  评论(0编辑  收藏  举报