1.MVC架构在JavaWeb中的应用
每个部分都有不同的职责和功能,以实现代码的分离和可维护性。
2.代码分离,来完成修改密码的功能
先从最底层的Dao写起:
updateUserPassword方法里:写sql语句,执行sql语句
package com.code.dao.user;
import com.code.dao.BaseDao;
import com.code.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDaoImpl implements UserDao{
public User getLoginUser(Connection connection, String userCode) throws Exception {
User user = null;
if(connection!=null){
String sql = "select* from smbms_user where userCode = ?";
Object[] params = {userCode};
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
resultSet = BaseDao.execute(connection, preparedStatement, sql, params,resultSet);
while(resultSet.next()){
user = new User();
user.setAddress(resultSet.getString("address"));
user.setUserCode(resultSet.getString("userCode"));
user.setUserName(resultSet.getString("userName"));
user.setUserPassword(resultSet.getString("userPassword"));
user.setGender(resultSet.getString("gender"));
user.setPhone(resultSet.getString("phone"));
user.setUserRole(resultSet.getInt("userRole"));
user.setCreatedBy(resultSet.getInt("createdBy"));
user.setId(resultSet.getInt("id"));
user.setModifyBy(resultSet.getInt("modifyBy"));
user.setBirthday(resultSet.getDate("birthday"));
user.setCreationDate(resultSet.getDate("creationDate"));
user.setModifyDate(resultSet.getDate("modifyDate"));
}
BaseDao.close(connection,preparedStatement,resultSet);
}
return user;
}
public boolean updateUserPassword(Connection connection, int id, String password) throws Exception {
boolean flag = false;
PreparedStatement statement = null;
if(connection!=null){
String sql = "update smbms_user SET userPassword=? WHERE id = ?";
Object[] params = {password, id};
flag = (boolean) BaseDao.execute(connection, statement, sql, params );
BaseDao.close(connection, statement, null);
}
return flag;
}
}
接下来是Service层:
获取数据库连接,把从Servlet层得到的参数,传给Dao
package com.code.service.user;
import com.code.dao.BaseDao;
import com.code.dao.user.UserDao;
import com.code.dao.user.UserDaoImpl;
import com.code.pojo.User;
import org.junit.Test;
import java.sql.Connection;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserServiceImpl() {
this.userDao = new UserDaoImpl();
}
public User login(String userCode, String password) {
User user = null;
Connection connection = null;
try {
connection = BaseDao.getConnection();
user = userDao.getLoginUser(connection, userCode);
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
BaseDao.close(connection, null, null);
}
return user;
}
public boolean updatePwd(int id, String password) {
boolean flag = false;
Connection connection = null;
try {
connection = BaseDao.getConnection();
flag = userDao.updateUserPassword(connection, id, password);
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
BaseDao.close(connection, null, null);
}
return flag;
}
@Test
public void test(){
UserServiceImpl userService = new UserServiceImpl();
User user = userService.login("admin", "kkkk");
System.out.println(user.getUserName());
System.out.println(user.getUserPassword());
}
}
最后要写的是Servlet:
从JSP中获取新密码,从Session中获取用户id,调用Service的更新密码方法,然后show message,完成页面跳转。
package com.code.servlet.user;
import com.code.pojo.User;
import com.code.service.user.UserService;
import com.code.service.user.UserServiceImpl;
import com.code.util.Constants;
import com.mysql.cj.util.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
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")) {
updatePwd(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
private void updatePwd(HttpServletRequest req, HttpServletResponse resp){
Object object = req.getSession().getAttribute(Constants.USER_SESSION);
String newPwd = req.getParameter("newpassword");
if(object!=null && !StringUtils.isNullOrEmpty(newPwd)){
UserService userService = new UserServiceImpl();
boolean res = userService.updatePwd(((User)object).getId(), newPwd);
if(res){
req.setAttribute(Constants.MESSAGE, "更新密码成功,请成功登录");
req.removeAttribute(Constants.USER_SESSION);
}else{
req.setAttribute(Constants.MESSAGE, "更新密码失败");
}
}else {
req.setAttribute(Constants.MESSAGE, "新密码异常");
}
try {
req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}