栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

超市订单管理系统SMBMS - 密码修改模块实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

超市订单管理系统SMBMS - 密码修改模块实现

文章目录

密码修改

功能实现流程编写密码修改前端代码UserDao接口UserDao接口实现类UserService接口UserService接口实现类Servlet实现复用

密码修改 功能实现流程

编写密码修改前端代码
<%@ 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" %>
UserDao接口
    //修改当前用户密码
    public int updatePwd(Connection connection,int id,int password) throws SQLException;
UserDao接口实现类
  //修改当前用户密码
    public int updatePwd(Connection connection, int id, int 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, pstm, sql, params);
            baseDao.closeResource(null, pstm, null);
        }
        return execute;
    }
}

UserService接口
//根据用户id修改密码
    public boolean updatePwd(int id, int pwd);
UserService接口实现类
public boolean updatePwd(int id, int 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;
    }
}
Servlet实现复用
package com.yang.servlet.user;

import com.mysql.jdbc.StringUtils;
import com.yang.pojo.User;
import com.yang.service.user.UserService;
import com.yang.service.user.UserServiceImpl;
import com.yang.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;

//实现servlet复用
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("savepwd")&&method!=null){
            this.updatePwd(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
        //从session里面拿ID;
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newpassword = req.getParameter("newpassword");

        boolean flag = false;

        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);
            }else {
                req.setAttribute("message","密码修改失败");

            }
        }else {
            req.setAttribute("message","新密码有问题");
        }
        req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/727356.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号