JavaWeb 项目实现(三) 修改密码功能

1.MVC架构在JavaWeb中的应用

每个部分都有不同的职责和功能,以实现代码的分离和可维护性。

  • JSP: 用户访问前端页面并发起请求,JSP将请求传递给Servlet
  • Servlet: 接收来自JSP的请求,调用Service,完成页面跳转
  • Service: 处理业务层代码
  • Dao: 访问数据库

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);
        }
    }
}

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值