整体思路:
在dao层写接口约束,在实现类中写乡音给的代码,传入的参数是用户的id,通过用户的id修改此用户的密码,返回参数类型为int,即有几条受影响的条数;
在service层中写service的接口约束,在实现类中调用dao层,这里的调用通常都是利用实例化service实现类时通过构造方法实例化dao,这里方法的参数是用户的id,返回类型用一个布尔值来表示是否修改成功即可;
编写servlet,在这里接收前段的请求参数,调用service层处理业务,一般是一些判断方法,旧密码是否正确等,最后返回视图。
在dao层,查找当前用户,将当前用户的密码进行修改
```java
@Override
public int updatePwd(Connection connection, int id, int password) throws Exception {
int updateRow=0;
PreparedStatement pstm=null;
if (connection!=null){
String sql="UPDATE `smbms_user` SET `userPassword`=? WHERE `id`=? ";
Object[] params={password,id};
BaseDao.execute(connection,sql,params,pstm);
}
BaseDao.closeResource(null,pstm,null);
return updateRow;
}
```
在service层,调用dao层,查找servlet层传来的数据的相关用户,并于servlet层数据进行对比
```java
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 (Exception e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return flag;
}
```
编写修改密码的servlet类,梳理整体逻辑
```java
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.modifyPwd(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 modifyPwd(HttpServletRequest req, HttpServletResponse resp){
Object attribute = req.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = req.getParameter("newpassword");
boolean flag = false;
if(attribute!=null && !StringUtils.isNullOrEmpty(newpassword)){
UserServiceImpl userService = new UserServiceImpl();
flag = userService.modifyPwd(((User) attribute).getId(), newpassword);
if(flag){
req.setAttribute("message","修改密码成功");
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();
}
}
```
优化密码使用ajax:
```java
public void pwdModify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Object o = req.getSession().getAttribute(Constant.USER_SESSION);
String oldpassword = req.getParameter("oldpassword");
Map<String, String> resultMap = new HashMap<String, String>();
if(null == o ){
resultMap.put("result", "sessionerror");
}else if(StringUtils.isNullOrEmpty(oldpassword)){
resultMap.put("result", "error");
}else{
String sessionPwd = ((User)o).getUserPassword();
if(oldpassword.equals(sessionPwd)){
resultMap.put("result", "true");
}else{
resultMap.put("result", "false");
}
}
resp.setContentType("application/json");
PrintWriter outPrintWriter = resp.getWriter();
outPrintWriter.write(JSONArray.toJSONString(resultMap));
outPrintWriter.flush();
outPrintWriter.close();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)