当然先写基本逻辑,前端就是在测试后端的路上完善的


修改密码
1.写UserDao 接口
//修改当前用户密码
public int updatePwd(Connection connection,int id,String pwd) throws SQLException;
2.写UserDao实现类
@Override
public int updatePwd(Connection connection, int id, String pwd) throws SQLException {
PreparedStatement pstm=null;
String sql="update smbms_user set userPassword = ? where id = ?";
Object[] param={pwd,id};
int i=0;
if (connection!=null){
i = BaseDao.execute(connection, pstm, sql, param);
BaseDao.closeResource(null,pstm,null);
}
return i;
}
3.业务层Service接口
//根据用户ID修改当前用户密码 Connection在Service层创造,所以在实现类里创造Connection,并且在里面关闭
public boolean updatePwd( int id, String pwd);
4.业务层Service实现类
//根据所给ID修改用户的密码
@Override
public boolean updatePwd(int id, String pwd) {
boolean flag =false;
Connection connection = null;
try {
connection=BaseDao.getConnection();
int i = userDao.updatePwd(connection, id, pwd);
if (i>0){
flag=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return flag;
}
5.Servlet控制层
package com.kuang.servlet.user;
import com.kuang.pojo.User;
import com.kuang.service.user.UserService;
import com.kuang.service.user.UserServiceIml;
import com.kuang.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 {
String method = req.getParameter("method");
if (method!=null&&method.equals("savepwd")){
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里拿对象
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 UserServiceIml();
flag = userService.updatePwd(((User) o).getId(), newpassword);
if (flag){
req.setAttribute(Constants.USER_message,"修改成功,请用新的密码登录");
req.getSession().removeAttribute(Constants.USER_SESSION);
}else {
req.setAttribute(Constants.USER_message,"修改失败");
}
}else {
req.setAttribute(Constants.USER_message,"新密码有问题");
}
try {
req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注册文件
<!-- 修改密码 -->
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.kuang.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
该文章描述了一个JavaWeb应用中修改用户密码的功能实现过程,涉及UserDao接口和实现类、业务层Service接口和实现、Servlet控制层以及数据库连接和操作。用户输入新密码后,通过Servlet处理请求,调用Service层更新密码,并反馈结果给前端。
791

被折叠的 条评论
为什么被折叠?



