JavaWeb 实现密码修改的案例

目录

一、基础环境搭建

二、编写Dao数据库操作层

三、编写服务层

四、编写视图控制器

五、jsp视图


一、基础环境搭建

见我的这篇博客:https://blog.csdn.net/qq_50909707/article/details/122609876

二、编写Dao数据库操作层

package com.wxl.dao.user;

import com.wxl.model.User;

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

public interface UserDao {

    //修改当前用户密码
    public int updatePwd(Connection connection, int id, String password) throws SQLException;
}

实现类

package com.wxl.dao.user;

import com.wxl.dao.BaseDao;
import com.wxl.model.User;

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

public class UserDaoImpl implements UserDao {

    //修改当前用户密码
    @Override
    public int updatePwd(Connection connection, int id, String password) throws SQLException {

        PreparedStatement pstm=null;
        int execute=0;
        if(connection!=null){
            String sql="update smbms_user set userPassword=? where id=?";
            Object params[]={password,id};
            execute=BaseDao.execute(connection,sql,params,pstm);
            BaseDao.closeResource(null,pstm,null);
        }
        return execute;
    }
}

三、编写服务层

package com.wxl.service.user;

import com.wxl.model.User;

public interface UserService {

    //根据用户ID修改密码
    public boolean updatePwd(int id,String password);
}

实现类

package com.wxl.service.user;

import com.wxl.dao.BaseDao;
import com.wxl.dao.user.UserDao;
import com.wxl.dao.user.UserDaoImpl;
import com.wxl.model.User;

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

public class UserServiceImpl implements UserService {

    @Override
    public boolean updatePwd(int id, String pwd) {
        Connection connection = null;
        boolean flag = false;
        //修改密码
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection, id, pwd) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
}

四、编写视图控制器

package com.wxl.controller.user;

import com.mysql.cj.util.StringUtils;
import com.sun.deploy.nativesandbox.NativeSandboxOutputStream;
import com.wxl.model.User;
import com.wxl.service.user.UserService;
import com.wxl.service.user.UserServiceImpl;
import com.wxl.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method=req.getParameter("method");
        if(method.equals("updatePwd")){
            updatePwd(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从Session里面拿ID
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newpassword = req.getParameter("newpassword");
        String oldpassword = req.getParameter("oldpassword");

        boolean flag = false;

        if (!((User) o).getUserPassword().equals(oldpassword)) {
            req.setAttribute("message", "原始密码错误");
            req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
        } else {
            if ((o != null) && (!StringUtils.isNullOrEmpty(newpassword))) {
                UserService userService = new UserServiceImpl();
                flag = userService.updatePwd(((User) o).getId(), newpassword);
                if (flag) {
                    req.setAttribute("message", "修改密码成功,请退出,使用新密码登录");
                    //密码修改成功,移除当前Session
                    //退出登录
                    req.getSession().removeAttribute(Constants.USER_SESSION);
                    resp.sendRedirect("/login.jsp");//返回登录页面
                } else {
                    req.setAttribute("message", "密码修改失败");
                    req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
                }
            } else {
                req.setAttribute("message", "修改出错");
                req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
            }
        }
    }
}

五、jsp视图

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/jsp/common/head.jsp"%>
你现在所在的位置是: 密码修改页面
${message}
<%@include file="/jsp/common/foot.jsp" %>

你可能感兴趣的:(javaWeb,java,java,jsp,服务器,javaweb,架构)