最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaWEB SMBMS實(shí)現(xiàn)密碼修改功能過程

 更新時(shí)間:2026年02月03日 08:56:40   作者:追JAVA的小菜鳥  
本文介紹了實(shí)現(xiàn)密碼修改功能的步驟,包括前端頁面、Dao層、Service層和Servlet層的編寫和調(diào)用,通過這些步驟,可以實(shí)現(xiàn)用戶密碼的修改功能

實(shí)現(xiàn)密碼修改功能

1、導(dǎo)入前端素材

密碼修改頁面——pwdmodify.jsp

2、Dao層——userDao接口

// 修改用戶密碼
public int updateUserPwd(Connection connection, int id, String userPassword) throws SQLException;

3、Dao層——userDaoImpl實(shí)現(xiàn)類

  • 編寫sql語句進(jìn)行修改操作——未傳實(shí)參
public int updateUserPwd(Connection connection, int id, String userPassword) throws SQLException {
    PreparedStatement preparedStatement = null;
    int row = 0;
    if (connection != null){
        String sql = "update smbms_user set userPassword = ? where id = ?";
        Object params[] = {userPassword,id};
        row = BaseDao.execute(connection, preparedStatement, sql, params);
        BaseDao.release(null,preparedStatement,null);
    }
    return row;
}

4、Service層——userService接口

// 修改用戶密碼
public Boolean updatePwd(int id,String password);

5、Service層——userServiceImpl實(shí)現(xiàn)類

  • 調(diào)用Dao層,實(shí)現(xiàn)修改密碼操作——未傳實(shí)參
// 修改用戶密碼
public Boolean updatePwd(int id, String password) {
    Connection connection = null;
    boolean flag = false;
    try {
        connection = BaseDao.getconnection();
        // 如果調(diào)用Dao層數(shù)方法,返回行數(shù)大于0,則更新成功
        if (userDao.updateUserPwd(connection, id, password)>0){
            flag = true;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        BaseDao.release(connection,null,null);
    }
    return flag;
}

6、Servlet層——UserServlet

  • 從頁面獲取參數(shù),調(diào)用userServiceImpl實(shí)現(xiàn)updatePwd()方法

完成Servlet的復(fù)用,調(diào)用各種方法

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 如果當(dāng)前頁面?zhèn)骰氐闹岛驮O(shè)定的method的值一樣,就進(jìn)入修改密碼方法
    String method = req.getParameter("method");
    if (method != null && method.equals("savepwd")) {
        this.UpdatePassword(req, resp);
    }else if(method.equals("pwdmodify")){
        this.pwdModify(req,resp);
    }
}

修改新密碼方法——UpdatePassword()

// 修改密碼——判斷新密碼
public void UpdatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    boolean flag = false;
    // 1. 從Session中獲取user的信息
    Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
    // 2. 從頁面中獲取新密碼
    String newpassword = req.getParameter("newpassword");
    // 3. 判斷——如果obj和輸入的密碼不為空 則進(jìn)行修改密碼操作(在數(shù)據(jù)庫中修改)
    if(obj != null&& newpassword != null){
        userServiceImpl userService = new userServiceImpl();
        // 4. 把obj強(qiáng)轉(zhuǎn)為User類型,拿到用戶ID信息,傳入方法中
        flag = userService.updatePwd(((User) obj).getId(), newpassword);
        System.out.println("修改密碼前:"+req.getSession().getAttribute(Constants.USER_SESSION));
        if(flag){
            // 若修改成功 移除已存在的Session
            req.setAttribute("message","密碼修改成功!");
            req.getSession().removeAttribute(Constants.USER_SESSION);
        }else {
            // 若修改失敗,展示錯(cuò)誤原因
            req.setAttribute("message","密碼修改失??!");
        }
    }else {
        req.setAttribute("message","新密碼為空,密碼修改失??!");
    }
    // 不管修改成功與否 都跳轉(zhuǎn)到當(dāng)前頁面,因?yàn)閟ession被清除了,所以沒有權(quán)限,會(huì)跳轉(zhuǎn)到error頁面
    resp.sendRedirect("pwdmodify.jsp");
    System.out.println("修改密碼后:"+req.getSession().getAttribute(Constants.USER_SESSION));
}

驗(yàn)證舊密碼——pwdModify()

// 修改密碼——驗(yàn)證舊密碼
public void pwdModify(HttpServletRequest req, HttpServletResponse resp) {
   // 1. 從Session中獲取user的信息
   Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
   // 2. 從頁面獲取輸入的舊密碼
   String oldpassword = req.getParameter("oldpassword");
   // 3. new 一個(gè)Map結(jié)果集
   HashMap<String, String> resultMap = new HashMap<String, String>();
   // 4. 若停留頁面時(shí)間過長,session失效,展示錯(cuò)誤信息
   if (obj == null) {
       resultMap.put("result", "sessionerror");
   } else if (oldpassword == null) {
       // 5. 若輸入的舊密碼為空,展示錯(cuò)誤信息
       resultMap.put("result", "error");
   } else {
       // 6. 獲取Session中的舊密碼,與輸入的舊密碼比較
       String userOldPassword = ((User) obj).getUserPassword();
       if (userOldPassword.equals(oldpassword)) {
           // 若相同,返回true
           resultMap.put("result", "true");
       } else {
           resultMap.put("result", "false");
       }
   }
   try {
       resp.setContentType("application/json");
       PrintWriter writer = resp.getWriter();
       //JSONArray 阿里巴巴的JSON工具類, 轉(zhuǎn)換格式
       /*
       resultMap = ["result","sessionerror","result","error"]
       Json格式 = {key:value}
        */
       writer.write(JSONArray.toJSONString(resultMap));
       writer.flush();
       writer.close();
   } catch (IOException e) {
       e.printStackTrace();
   }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

当雄县| 阿尔山市| 水城县| 车致| 兰坪| 大港区| 民乐县| 博湖县| 于田县| 巴塘县| 本溪市| 金沙县| 东丰县| 乌拉特后旗| 沈丘县| 高碑店市| 德昌县| 永年县| 凤翔县| 盱眙县| 香港 | 土默特右旗| 平湖市| 扎囊县| 方山县| 景德镇市| 抚松县| 宝丰县| 峨山| 淳安县| 米脂县| 逊克县| 古蔺县| 仁寿县| 罗江县| 金阳县| 开江县| 田东县| 平顺县| 松桃| 新泰市|