超市订单管理系统,修改密码功能实现

建议从底层向上写

步骤:

1.UserDao接口

    //修改用户密码
    int updatePwd(Connection connection,int id,int password) throws SQLException;

 

 2.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.smbms_user set userPassword=? where id = ?";
            Object[] params={password,id};
            execute = BaseDao.execute(connection, pstm, sql, params);
            BaseDao.closeResource(null,pstm,null);

        }
        return execute;
  }

 

3,UserService接口

    //根据用户id修改密码
    boolean updatePwd(int id,int pwd);

 

 

 4,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 throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

 5 ,servlet层

package com.king.servlet.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.king.pojo.User;
import com.king.service.user.UserService;
import com.king.service.user.UserServiceImpl;
import com.king.util.Constants;
import com.mysql.cj.util.StringUtils;
import com.sun.org.apache.xpath.internal.objects.XObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

//实现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);
        }else if (method.equals("pwdmodify")&&method!= null){
            this.pwdModify(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&& newpassword!=null){
                UserService userService = new UserServiceImpl();
                //object对象转换成user对象,最后拿id
                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","新密码有问题");
            }

            try {
                req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        //验证旧密码,session中有用户旧密码
    public void pwdModify(HttpServletRequest req, HttpServletResponse resp){
        //从session中拿到用户id
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String oldpassword = req.getParameter("oldpassword");

        //万能的Map : 结果集
        Map<String,String> resultMap = new HashMap<String,String>();

        if(o==null){//Session失败了,session过期了
            resultMap.put("result","sessionerror");
        }else if (StringUtils.isNullOrEmpty(oldpassword)){//输入密码为空
            resultMap.put("result","error");
        }else {
            String userPassword = ((User) o).getUserPassword();//session中用户的面膜
            if(oldpassword.equals(userPassword)){
                resultMap.put("result","true");
            }else{
                resultMap.put("result","false");
            }
        }

        try {
            resp.setContentType("application/json");
            PrintWriter writer = resp.getWriter();
            //JSONArray 阿里巴巴的JSON工具类,转换格式
            /*
            * resultMap=["result","sessionerror","result","error"]
            * Json格式={key:value}
            * */
            writer.write(JSONArray.toJSONString(resultMap));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

6 web配置

    <!--默认session过期时间-->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <servlet>
        <servlet-name>LogoutServlet</servlet-name>
        <servlet-class>com.king.servlet.user.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogoutServlet</servlet-name>
        <url-pattern>/jsp/logout.do</url-pattern>
    </servlet-mapping>

修改密码(session方面)

1.首先验证旧密码(在登陆时,用户的所有信息已经在dao层全部获取到,通过service-->servlet,存入session中),当session中的验证码和用户输入的相同时,代表旧密码验证成功,否则失败,重新输入

2.确认新密码,通过前端jsp页面判断两次用户输入的新密码是否相同,相同,将newpassword传到后端(过程,session->servlet->service-->dao),通过basedao这个工具类设置newpassword

posted @ 2020-10-12 10:39  凸然猿  阅读(728)  评论(0编辑  收藏  举报