实现修改密码功能
1.UserDao接口
//修改当前用户密码
public int updatePwd(Connection connection,int id,int password)throws SQLException;
2.UserDao接口实现类
public int updatePwd(Connection connection, int id, int password) throws SQLException {
//用来执行预编译
PreparedStatement preparedStatement = null;
int execute=0;
if (connection!=null){
String sql = "update smbms_user set userPassword = ? where id = ?";
Object params[]={
password,id};
execute = BaseDao.execute(connection, preparedStatement, sql, params);
}
return execute;
}
3.UserService接口
//根据用户id,修改密码
public boolean updatePwd( int id, int password);
4.UserService接口实现类
public boolean updatePwd(int id, int password) {
Connection connection = null;
boolean flag=false;
//修改密码
try {
connection= BaseDao.getConnection();
if (userDao.updatePwd(connection,id,password)>0){
//修改成功
flag=true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
BaseDao.closeResources(connection,null,null);
}
return flag;
}
5. UserServlet
package com.hao.servlet.user;
import com.hao.pojo.User;
import com.hao.service.user.UserService;
import com.hao.service.user.UserServiceImpl;
import com.hao.util.Constants;
import com.mysql.jdbc.StringUtils;
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 {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if (method!=null&&method.equals("savepwd")){
this.updatePwd(req,resp);
}
}
public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
//从Session中获取用户id (用户登陆时,所有信息都已存入Session中)
Object attribute = req.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = req.getParameter("newpassword");
boolean flag =false;
if (attribute!=null&& !StringUtils.isNullOrEmpty(newpassword)){
UserService userService = new UserServiceImpl();
flag = userService.updatePwd(((User)attribute).getId(),newpassword);
if (flag){
req.setAttribute("massage","密码修改成功,请重新登录");
//密码修改成功,移除当前Session
req.getSession().removeAttribute(Constants.USER_SESSION);
}else {
req.setAttribute("massage","密码修改失败");
}
}else {
req.setAttribute("massage","新密码有问题");
}
try {
req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.在Web.xml中注册
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.hao.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
7.优化(验证旧密码)
//验证旧密码,session中有旧密码,不用重新从底层写起
public void pwdModify(HttpServletRequest req, HttpServletResponse resp){
//从Session中拿到用户
Object object = req.getSession().getAttribute(Constants.USER_SESSION);
String oldpassword = req.getParameter("oldpassword");
//万能Map 结果集
HashMap<String, String> resultMap = new HashMap<String,String>();
if(object==null){
//session失效或过期
resultMap.put("result","sessionerror");
}else if (StringUtils.isNullOrEmpty(oldpassword)){
//输入的密码为空
resultMap.put("result","error");
}else {
//session中用户的密码
String userPassword = ((User) object).getUserPassword();
if (oldpassword.equals(userPassword)){
resultMap.put("result","true");
}else {
resultMap.put("result","false");
}
}
try {
resp.setContentType("application/json");
PrintWriter writer = resp.getWriter();
//JSONArray 阿里巴巴的Json工具类 用来转换格式
/*
resultMap=["result","sessionerror","result","error"]->
Json格式={key:value}
*/
writer.write(JSONArray.toJSONString(resultMap));
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
8.测试访问
将密码改为八个8
查看数据库中数据
确实已经改变