2024/02/05

开始设计数据库操作方法来修改密码

    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;
    }

然后通过servlet来进行调用完成密码修改,修改完成后将会直接重定向返回主页面并给出提示。

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);
    }
}

然后在注册账号时我们还要添加一个操作来判断该用户名是否已经被注册,这刚好也要用到我们之前写的查找账号的方法。

修改完成的servlet为

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="/register-servlet")
public class FegisterServlet 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("pwd1");
        hhh=new Base_InformationBean();
        hhh.setCode(code);
        hhh.setPassword(password);
        if(new InfoDAO().findCode(hhh))
        {
            request.setAttribute("msg","该账号已被注册请重新填写");
            try {
                request.getRequestDispatcher("register.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            new InfoDAO().register(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);
    }
}

这样我们就完成了忘记密码时的密码修改和补充了账号重复注册时会出现问题的bug

 

posted @ 2024-02-05 17:51  伐木工熊大  阅读(1)  评论(0编辑  收藏  举报