這個功能只在servlet做就可以了,這個是和修改密碼一塊的servlet
1.引入json格式轉換的依賴,引入依賴后如果沒有自動加載,就右鍵專案-》maven-》reload project
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>fastjson</artifactId> 4 <version>1.2.9</version> 5 </dependency>
2.撰寫servlet
1 package com.xiaoma.servlet.user; 2 3 import com.mysql.jdbc.StringUtils; 4 import com.xiaoma.pojo.User; 5 import com.xiaoma.service.user.UserService; 6 import com.xiaoma.service.user.UserServiceImpl; 7 import com.xiaoma.util.Constants; 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import java.io.IOException; 13 import java.io.PrintWriter; 14 import java.util.HashMap; 15 16 import com.alibaba.fastjson.*; 17 18 public class UserServlet extends HttpServlet { 19 @Override 20 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 String method = req.getParameter("method"); 22 if (method != null&&method.equals("savepwd")) { 23 this.updataPwd(req,resp); 24 }else if(method!=null && method.equals("pwdmodify")){ 25 this.pwdModify(req,resp); 26 } 27 } 28 29 @Override 30 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 31 doGet(req, resp); 32 } 33 34 //修改密碼,把方法提出來,實作servlet的復用 35 public void updataPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 //獲取用戶session 37 Object o = request.getSession().getAttribute(Constants.USER_SESSION); 38 //拿到前端的newpassword引數 39 String newpassword = request.getParameter("newpassword"); 40 System.out.println("Servlet:"+newpassword); 41 boolean flag = false; 42 //當密碼不為空并且session也不為空往下走 43 if (o != null && !StringUtils.isNullOrEmpty(newpassword)) { 44 UserService userService = new UserServiceImpl(); 45 //拿到用戶id和密碼 46 flag = userService.updatePwd(((User) o).getID(), newpassword); 47 if (flag) { 48 request.setAttribute(Constants.USER_SESSION, "修改密碼成功,請退出并使用新密碼重新登錄!"); 49 //如果密碼修改成功就吧session移除掉 50 request.getSession().removeAttribute(Constants.USER_SESSION);//session注銷 51 } else { 52 request.setAttribute(Constants.USER_SESSION, "修改密碼失敗!"); 53 } 54 } else { 55 request.setAttribute(Constants.USER_SESSION, "修改密碼失敗!"); 56 } 57 request.getRequestDispatcher("pwdmodify.jsp").forward(request, response); 58 } 59 60 //驗證舊密碼,我們不用再從資料庫里查找舊密碼,因為我們登錄了已經,那么session中就會存在密碼,就可以通過session獲取登錄密碼 61 //所以我們只要將session中的密碼與前端輸入的密碼進行對比就可以了 62 public void pwdModify(HttpServletRequest req, HttpServletResponse resp){ 63 //獲取session中的id 64 Object o = req.getSession().getAttribute(Constants.USER_SESSION); 65 //獲取js中的ajax請求中的pwdmodify,這個驗證舊密碼的ajax中的data:{method:"pwdmodify",oldpassword:oldpassword.val()} 66 //其中method就相當于一個key,oldpassword是獲取的前端的值 67 String oldpassword=req.getParameter("oldpassword"); 68 //使用map存放ajax請求中的那好幾個資料 69 HashMap<String, String> resultMap = new HashMap<String,String>(); 70 71 //如果獲取的session物件o為空,說明session過期了或者失效了 72 if (o == null) { 73 //result為pwdmodify.js中的舊密碼驗證ajax中的result,sessionerror為session過期時對應的請求 74 resultMap.put("result","sessionerror"); 75 }else if(StringUtils.isNullOrEmpty(oldpassword)){ 76 //當用戶輸入的舊密碼為空的時候 77 resultMap.put("result","error"); 78 }else{ 79 //當密碼不為空且session存活的時候,就去拿到session中的密碼 80 String userPassword = ((User) o).getUserPassword(); 81 //當session中的密碼和舊密碼一樣的時候,就成功,否則就失敗 82 if (userPassword.equals(oldpassword)) { 83 resultMap.put("result","true"); 84 }else{ 85 resultMap.put("result","false"); 86 } 87 } 88 89 //下面就是把資料給Ajax接收,先將map資料變為json資料 90 try { 91 resp.setContentType("application/json"); 92 PrintWriter writer = resp.getWriter(); 93 //將map集合中的資料轉為json字串 94 writer.write(JSONArray.toJSONString(resultMap)); 95 writer.flush(); 96 writer.close(); 97 } catch (IOException e) { 98 e.printStackTrace(); 99 } 100 } 101 }
效果圖:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335052.html
標籤:Java
上一篇:Mybatis 動態批量修改
下一篇:軟體架構設計原則之里氏替換原則
