密码修改
1.导入前端素材
<li><a href="${pageContext.request.contextPath }/jsp/pwdmodify.jsp">密码修改</a></li>
2.写项目,建议从底层向上写
3.UserDao接口
//修改当前用户密码
int updatePwd(Connection connection,int id,String password) throws SQLException;
4.UserDao接口实现类
//修改当前用户密码
@Override
public int updatePwd(Connection connection, int id, String 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;
}
5.UserService层
//根据用户id修改密码
boolean updatePwd(int id, String password);
6.UserService层实现类
//根据用户id修改密码
@Override
public boolean updatePwd(int id, String password) {
Connection connection = null;
boolean flag = false;
//修改密码
try {
connection = BaseDao.getConnection();
if (userDao.updatePwd(connection, id, password) > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeResource(connection, null, null);
}
return flag;
}
7.记得实现复用,需要提取出方法
package com.gongyi.servlet.user;
import com.gongyi.pojo.User;
import com.gongyi.service.user.UserService;
import com.gongyi.service.user.UserServiceImpl;
import com.gongyi.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.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");
System.out.println("UserServlet:newpassword" + newpassword);
boolean flag = false;
System.out.println(o != null);
System.out.println(!StringUtils.isNullOrEmpty(newpassword));
if (o != null && newpassword != null) {
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", "新密码有问题");
}
try {
req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在web.xml中注册Servlet
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.gongyi.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
8.测试
效果图:
彩蛋
1.调试技巧之打印关键变量法
2.idea的tomcat 热更新
3.前端调试之清除浏览器缓存
4.前端js文件乱码
js文件本身拖拽到浏览器无乱码
怀疑是tomcat在搞鬼,
在tomcat启动时配置vm参数:
-Dfile.encoding=UTF-8
完美解决
5.jsp图片文件找不到
解决:
common.js
原始代码:
var imgYes = "<img width='15px' src="+path+"'/images/y.png' />";
var imgNo = "<img width='15px' src="+path+"'/images/n.png' />";
改为:去掉多余的单引号
var imgYes = "<img width='15px' src="+path+"/images/y.png />";
var imgNo = "<img width='15px' src="+path+"/images/n.png />";
然后重启服务,清除浏览器缓存就可以了
效果图: