33.smbms密码修改实现
- 导入前端素材
<li><a href="${pageContext.request.contextPath }/jsp/pwdmodify.jsp">密码修改</a></li>
写项目,建议从底层向上写
- UserDao接口
updatePwd.java
//修改当前用户密码
public int updatePwd(Connection connection, int id, String pwd) throws Exception;
- UserDao接口实现类
updatePwd.java
//修改当前用户密码
public int updatePwd(Connection connection, int id, String pwd) throws Exception {
int flag = 0;
PreparedStatement pstm = null;
if (connection != null) {
String sql = "update smbms_user set userPassword= ? where id = ?";
Object[] params = {pwd, id};
flag = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null, pstm, null);
}
return flag;
}
- UserService层
//根据用户Id修改密码
public boolean updatePwd(int id, String pwd);
- UserService实现类
public boolean updatePwd(int id, String pwd) {
boolean flag = false;
Connection connection = null;
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记得实现复用,要提取出方法!在 dao层service层下面是 servlet层 的主体
//实现Servlet的复用
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从Session里面拿ID
Object o = request.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = request.getParameter("newpassword");
boolean flag = false;
if (o != null && !StringUtils.isNullOrEmpty(newpassword)) {
UserService userService = new UserServiceImpl();
flag = userService.updatePwd(((User) o).getId(), newpassword);
if (flag) {
request.setAttribute("message", "修改密码成功,请退出并使用新密码重新登录!");
request.getSession().removeAttribute(Constants.USER_SESSION);//session注销
} else {
request.setAttribute("message", "修改密码失败!");
}
} else {
request.setAttribute("message", "修改密码失败!");
}
//修改成功后转发就行了
request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
}
- 提取为方法
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter( "method" );
if(method.equals( "savepwd" ) && method != null){
this.updatePwd( request,response );
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet( req, resp );
}
public void updatePwd(HttpServletRequest request, HttpServletResponse response){
//从Session里面拿ID
Object o = request.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = request.getParameter("newpassword");
boolean flag = false;
if (o != null && !StringUtils.isNullOrEmpty(newpassword)) {
UserService userService = new UserServiceImpl();
flag = userService.updatePwd(((User) o).getId(), newpassword);
if (flag) {
request.setAttribute("message", "修改密码成功,请退出并使用新密码重新登录!");
request.getSession().removeAttribute(Constants.USER_SESSION);//session注销
} else {
request.setAttribute("message", "修改密码失败!");
}
} else {
request.setAttribute("message", "修改密码失败!");
}
//修改成功后转发就行了
try {
request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注册xml
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.study.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
- 测试