整体思路:
在dao层写接口约束,在实现类中写乡音给的代码,传入的参数是用户的id,通过用户的id修改此用户的密码,返回参数类型为int,即有几条受影响的条数;
在service层中写service的接口约束,在实现类中调用dao层,这里的调用通常都是利用实例化service实现类时通过构造方法实例化dao,这里方法的参数是用户的id,返回类型用一个布尔值来表示是否修改成功即可;
编写servlet,在这里接收前段的请求参数,调用service层处理业务,一般是一些判断方法,旧密码是否正确等,最后返回视图。
在dao层,查找当前用户,将当前用户的密码进行修改
```java
//修改当前用户密码,传入的参数用于修改密码
//增删改都会影响数据库的变化,所以是返回int类型,说明有几行受到了影响
@Override
public int updatePwd(Connection connection, int id, int password) throws Exception {
int updateRow=0;
PreparedStatement pstm=null; //预编译sql对象
if (connection!=null){//判断是否连接
String sql="UPDATE `smbms_user` SET `userPassword`=? WHERE `id`=? ";//编写sql语句
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){
//获取要修改的密码和id
Object attribute = req.getSession().getAttribute(Constants.USER_SESSION);//从session中获取保存的user
String newpassword = req.getParameter("newpassword");//获取新密码
boolean flag = false;
//判断这个session和新密码是否存在
if(attribute!=null && !StringUtils.isNullOrEmpty(newpassword)){
UserServiceImpl userService = new UserServiceImpl();
flag = userService.modifyPwd(((User) attribute).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();
}
}
```
优化密码使用ajax:
```java
public void pwdModify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//从Session中拿ID
Object o = req.getSession().getAttribute(Constant.USER_SESSION);
String oldpassword = req.getParameter("oldpassword");
//万能的Map : 结果集
Map<String, String> resultMap = new HashMap<String, String>();
if(null == o ){//session过期、失效
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();
}
标签:修改,resp,req,密码,null,id
From: https://www.cnblogs.com/hytip/p/18182287