原生javaweb开发-----超市管理系统(三):修改密码

在这里插入图片描述

依旧从底层往上写

1、导入修改密码页面的前端资源

<%@ 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 }/jsp/user.do">
                    <input type="hidden" name="method" value="savepwd">
                     <input type="hidden" name="id"  value="${user.id })"> 
                    <!--div的class 为error是验证错误,ok是验证成功-->
                    <div class="info">${message}</div>
                    <div class="">
                        <label for="oldPassword">旧密码:</label>
                        <input type="password" name="oldpassword" id="oldpassword" value=""> 
						<font color="red"></font>
                    </div>
                    <div>
                        <label for="newPassword">新密码:</label>
                        <input type="password" name="newpassword" id="newpassword" value=""> 
						<font color="red"></font>
                    </div>
                    <div>
                        <label>确认新密码:</label>
                        <input type="password" name="rnewpassword" id="rnewpassword" value=""> 
						<font color="red"></font>
                    </div>
                    <div class="providerAddBtn">
                        <!--<a href="#">保存</a>-->
                        <input type="button" name="save" id="save" value="保存" class="input-button">
                    </div>
                </form>
            </div>
        </div>
    </section>
<%@include file="../jsp/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/pwdmodify.js"></script>

在这里插入图片描述

2、userDao接口添加修改方法

这里有三个参数:connection连接对象,数据库中用户id,以及密码Password。

public interface UserDao {
    //得到要登录的用户
    public User getLoginUser(Connection connection,String userCode) throws SQLException;
    //修改密码
    public int updatePwd(Connection connection,int id,String password) throws SQLException;
}
2.1在接口实现类userDaoImpl里面重写修改方法、

数据库查询公共类的增删改方法,需传入四个参数:connection连接对象,preparedStatement执行对象,sql语句,和parms参数。

public int updatePwd(Connection connection, int id, String password) throws SQLException {

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

3、业务层UserService接口添加方法

业务层需要的参数只有数据库中的id,和password;

public interface UserService {
    //用户登录
    public User login(String userCode,String password);
    //修改密码
    public boolean updatePwd(int id, String password) throws SQLException;

}
3.1在接口实现类userServiceImpl里面重写方法

首先通过数据库公共操作类连接到数据库,然后调用DAO层接口实现updatePwd()修改方法,如果该方法执行成功,数据库则修改完成,返回真。


    public boolean updatePwd(int id, String password) throws SQLException {
       Connection connection=null;
       boolean flag=false;

       connection = BaseDao.getConnection();
       //修改密码
        if (userDao.updatePwd(connection,id,password)>0){
            flag=true;
        }
        BaseDao.closeResource(null,null,connection);
        return flag;
    }

4、控制层UserServlet

业务层实现方法需要两个参数id和password,这里的password由前端传递过来,由于在登录成功时将用户信息存到了session中,所以现在可以从seesion中拿到所需要的id.
当用户session中返回的内容不为空且从前端拿到的参数不为空时,调用业务层的updatePwd()方法一直走到底层修改数据库,当返回的结果为true时,提示修改成功,同时移除session内容,修改失败时,返回到修改页面。

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //从session中拿到用户id
        Object att = req.getSession().getAttribute(Constants.USER_SESSION);
        String newpassword = req.getParameter("newpassword");

        boolean flag=false;

        if (att!=null&& !StringUtils.isNullOrEmpty(newpassword)){
            UserService userService=new UserServiceImpl();
            try {
                flag = userService.updatePwd(((User) att).getId(), newpassword);
                if (flag){
                    req.setAttribute("message","修改成功");
                    req.getSession().removeAttribute(Constants.USER_SESSION);
                }else{
                    req.setAttribute("message","修改失败");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else {
            req.setAttribute("message","新密码有问题");
        }
        req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
    }

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

5、web.xml注册

 <!--UserServlet注册-->
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.tt.servlet.user.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/jsp/user.do</url-pattern>
    </servlet-mapping>
修改前:

在这里插入图片描述

修改前:

在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值