(servlet)
//实现servlet复用 public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从session获取id Object o=req.getSession().getAttribute(Constants.USER_SESSION); String newpassword=req.getParameter("newpassword"); boolean flag=false; System.out.println("newpassword1"+newpassword); if(o!=null&&!StringUtils.isNullOrEmpty(newpassword)) { UserService userService=new UserServiceImp1(); flag=userService.updatePwd(((User)o).getId(),newpassword); if(flag) { System.out.println("newpassword flag"+newpassword); req.setAttribute("message","修改密码成功 请重新登录"); //修改完密码 移除session req.getSession().removeAttribute(Constants.USER_SESSION); } else { req.setAttribute("message","修改密码失败"); } } else { req.setAttribute("message","新密码有问题"); } req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp); }
(service)
public boolean updatePwd(int id, String password) { Connection connection=null; boolean flag=false; System.out.println("newpassword service"+password); try { connection=BaseDao.getConnection(); if(userDao.updatePwd(connection,id,password)>0) { flag=true; } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { BaseDao.closeResource(connection,null,null); } return flag; }
(dao)
public int updatePwd(Connection connection, int id, String password) { PreparedStatement pstm=null; //execute执行了几行的改动 int execute=0; if(connection!=null) { String sql="update smbms_user set userPassword = ? where id =?"; Object params[]={password,id}; System.out.println("newpassword dao"+password); try { execute=BaseDao.execute(connection,sql,params,pstm); } catch (Exception e) { e.printStackTrace(); } BaseDao.closeResource(null,pstm,null); } return execute; }