JavaWeb 实现密码修改的案例

29 篇文章 1 订阅

目录

一、基础环境搭建

二、编写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"%>
<div class="right">
            <div class="location">
                <strong>你现在所在的位置是:</strong>
                <span>密码修改页面</span>
            </div>
            <div class="providerAdd">
                <form id="userForm" name="userForm" method="post" action="${pageContext.request.contextPath }/user/user.do">
                    <input type="hidden" name="method" value="updatePwd">
                    <!--div的class 为error是验证错误,ok是验证成功-->
                    <div class="info">${message}</div>
                    <div class="">
                        <label for="oldPassword">旧密码:</label>
                        <input required type="password" name="oldpassword" id="oldpassword" value="">
						<font color="red"></font>
                    </div>
                    <div>
                        <label for="newPassword">新密码:</label>
                        <input required type="password" name="newpassword" id="newpassword" value="">
						<font color="red"></font>
                    </div>
                    <div>
                        <label for="rnewpassword">确认新密码:</label>
                        <input required type="password" name="rnewpassword" id="rnewpassword" value="">
						<font color="red"></font>
                    </div>
                    <div class="providerAddBtn">
                        <!--<a href="#">保存</a>-->
                        <button hidden id="button">button</button>
                    </div>
                </form>
                <input type="submit" onclick="handle()" name="save" id="save" value="保存" class="input-button">
            </div>
        </div>
    </section>
<%@include file="/jsp/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/pwdmodify.js"></script>
  • 10
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值