JavaWEB SMBMS實(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í)間過長(zhǎng),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)文章
Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù)
這篇文章主要介紹了Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
這篇文章主要介紹了SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
基于<aop:aspect>與<aop:advisor>的區(qū)別
這篇文章主要介紹了<aop:aspect>與<aop:advisor>的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Boot修改內(nèi)置Tomcat默認(rèn)端口號(hào)的示例
本篇文章主要介紹了Spring Boot修改內(nèi)置Tomcat端口號(hào)的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
詳解Java對(duì)象序列化為什么要使用SerialversionUID
這篇文章主要介紹了詳解Java對(duì)象序列化為什么要使用SerialversionUID,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
spring利用squertz實(shí)現(xiàn)定時(shí)任務(wù)
spring squertz是一個(gè)強(qiáng)大的定時(shí)任務(wù)處理方式。下面這篇文章主要介紹了spring利用squertz實(shí)現(xiàn)定時(shí)任務(wù)的相關(guān)資料,文中介紹的很詳細(xì),對(duì)大家具有一定的參考借鑒價(jià)值,需要的朋友們下面來一起看看吧。2017-01-01
Java中生產(chǎn)者消費(fèi)者問題總結(jié)
這篇文章主要介紹了Java中生產(chǎn)者消費(fèi)者問題總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
mybatis實(shí)現(xiàn)一對(duì)一關(guān)聯(lián)映射實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于mybatis實(shí)現(xiàn)一對(duì)一關(guān)聯(lián)映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

