超市管理系统总结篇三用户密码修改
- UserDao接口
//修改当前用户密码
public int updatePwd(Connection connection,int id,int password)throws SQLException, Exception;
12
- UserDao接口实现类
@Override//修改当前密码
public int updatePwd(Connection connection, int id, int password) throws Exception {
// TODO 自动生成的方法存根
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;
}
12345678910111213
- UserService层
public boolean updatePwd(int id,int password)throws SQLException, Exception;
1
- UserService实现类
public boolean updatePwd(int id, int password) throws SQLException, Exception {
// TODO 自动生成的方法存根
Connection connection = null;
boolean flag = false;
//修改密码
try {
connection = BaseDao.getConnection();
if(userDao.updatePwd(connection, id, password)>0) {
flag = true;
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
BaseDao.closeResource(connection, null, null);
}
return flag;
}
12345678910111213141516171819
- servlet记得实现复用,要提取出方法!
在 dao层 和 service层 自己写映射类和实现类
下面是 servlet层 的主体
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
String method = req.getParameter("method");
if (method != "savepwd" && method != null) {
this.updatePwd(req, resp);
}
//实现复用~~~~~~
// 想添加新的增删改查,直接用if(method != "savepwd" && method != null);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
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 && newpassword != null) {
UserService userService = new UserServiceImpl();
try {
flag = userService.updatePwd(((User) o).getId(), newpassword);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
if (flag) {
req.setAttribute("message", "密码修改成功,请退出,使用新密码登录");
// 密码修改成功,移除session(移除后不能再次修改密码,建议不移除)
req.getSession().removeAttribute(Constants.USER_SESSION);
} else {
// 密码修改失败
req.setAttribute("message", "密码修改失败");
}
} else {
// 密码修改有问题
req.setAttribute("message", "新密码有问题");
}
try {
req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
} catch (ServletException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
注册xml
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
12345678
- 测试
优化密码修改使用Ajax
阿里巴巴的fastjson
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
2.后台代码修改
导入阿里的包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
123456
import java.io.IOException;
import java.io.PrintWriter;
//import java.io.Writer;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.cj.util.StringUtils;
import com.mysql.cj.xdevapi.JsonArray;
//import com.mysql.cj.util.StringUtils;
import pojo.User;
import service.user.UserService;
import service.user.UserServiceImpl;
import util.Constants;
@SuppressWarnings("serial")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
String method = req.getParameter("method");
if (method.equals( "savepwd") && method != null) {
this.updatePwd(req, resp);
}else if (method.equals( "pwdmodify") && method != null) {
}
// 想添加新的增删改查,直接用if(method != "savepwd" && method != null);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
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 && newpassword != null) {
UserService userService = new UserServiceImpl();
try {
flag = userService.updatePwd(((User) o).getId(), newpassword);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
if (flag) {
req.setAttribute("message", "密码修改成功,请退出,使用新密码登录");
// 密码修改成功,移除session(移除后不能再次修改密码,建议不移除)
req.getSession().removeAttribute(Constants.USER_SESSION);
} else {
// 密码修改失败
req.setAttribute("message", "密码修改失败");
}
} else {
// 密码修改有问题
req.setAttribute("message", "新密码有问题");
}
try {
req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
} catch (ServletException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void pqdmodify(HttpServletRequest req, HttpServletResponse resp) {
// 通过session获得用户id
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
String oldpassword = req.getParameter("oldpassword");
Map<String, String> resultMap = new HashMap<String, String>();
if(o==null) {//session失效,session过期了
resultMap.put("result","seesionerror");
}else if(StringUtils.isNullOrEmpty(oldpassword)){//输入密码为空
resultMap.put("result","error");
}else {//
String userPassword = ((User)o).getUserPassword();//seesion中的用户密码
if(oldpassword.equals(userPassword)) {
resultMap.put("result","true");
}else {
resultMap.put("result","false");
}
}
try {
resp.setContentType("application/josn");
PrintWriter writer = resp.getWriter();
/*
* resultMap = ["result","sessionerror","result",error]
* josn格式={key,value
*/
//writer.write(JSONArray.toJSONString(resultMap));
writer.write(JsonArray.class.toString());
writer.flush();
writer.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}