密码修改
功能实现流程编写密码修改前端代码UserDao接口UserDao接口实现类UserService接口UserService接口实现类Servlet实现复用
密码修改 功能实现流程 编写密码修改前端代码<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/jsp/common/head.jsp"%>UserDao接口<%@include file="/jsp/common/foot.jsp" %>你现在所在的位置是: 密码修改页面
//修改当前用户密码
public int updatePwd(Connection connection,int id,int password) throws SQLException;
UserDao接口实现类
//修改当前用户密码
public int updatePwd(Connection connection, int id, int password) throws SQLException {
PreparedStatement pstm = null;
int execute = 0;
if (connection!=null) {
String sql = "update smbms_user set userPassword = ? where id = ?";
Object params[] = {password, id};
execute = baseDao.execute(connection, pstm, sql, params);
baseDao.closeResource(null, pstm, null);
}
return execute;
}
}
UserService接口
//根据用户id修改密码
public boolean updatePwd(int id, int pwd);
UserService接口实现类
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 (SQLException e) {
e.printStackTrace();
}finally {
baseDao.closeResource(connection,null,null);
}
return flag;
}
}
Servlet实现复用
package com.yang.servlet.user;
import com.mysql.jdbc.StringUtils;
import com.yang.pojo.User;
import com.yang.service.user.UserService;
import com.yang.service.user.UserServiceImpl;
import com.yang.util.Constants;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//实现servlet复用
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.updatePwd(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
//从session里面拿ID;
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = req.getParameter("newpassword");
boolean flag = false;
if (o!=null&& !StringUtils.isNullOrEmpty(newpassword)){
UserService userService = new UserServiceImpl();
flag = userService.updatePwd(((User) o).getId(), newpassword);
if (flag){
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);
}
}



