SMBMS-修改密码实现

1、编写pwdmodify.jsp密码修改页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/jsp/common/head.jsp"%>
<div class="right">
            <div class="location">
                <strong>你现在所在的位置是:</strong>
                <span>密码修改页面</span>
            </div>
            <div class="providerAdd">
                <form id="userForm" name="userForm" method="post" action="${pageContext.request.contextPath }/jsp/user.do">
                    <input type="hidden" name="method" value="savepwd">
                    <!--div的class 为error是验证错误,ok是验证成功-->
                    <div class="info">${message}</div>
                    <div class="">
                        <label for="oldPassword">旧密码:</label>
                        <input type="password" name="oldpassword" id="oldpassword" value=""> 
						<font color="red"></font>
                    </div>
                    <div>
                        <label for="newPassword">新密码:</label>
                        <input type="password" name="newpassword" id="newpassword" value=""> 
						<font color="red"></font>
                    </div>
                    <div>
                        <label for="reNewPassword">确认新密码:</label>
                        <input type="password" name="rnewpassword" id="rnewpassword" value=""> 
						<font color="red"></font>
                    </div>
                    <div class="providerAddBtn">
                        <!--<a href="#">保存</a>-->
                        <input type="button" name="save" id="save" value="保存" class="input-button">
                    </div>
                </form>
            </div>
        </div>
    </section>
<%@include file="/jsp/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/pwdmodify.js"></script>
2、userDao接口 增加用户修改密码方法
package com.smbms.dao.user;
import com.smbms.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;

public interface UserDao {
    //得到要登录的用户
    ......
    //修改用户密码
    public  int updateUserPassword(Connection connection,int id,String password) throws SQLException;
}
3、在userDao接口实现类中增加用户修改密码方法实现
package com.smbms.dao.user;

import com.smbms.dao.BaseDao;
import com.smbms.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    //获取用户登录
    .........
      
    //修改用户密码
    public int updateUserPassword(Connection connection, int id, String password) throws SQLException {
        PreparedStatement preparedStatement = null;
        int execute = 0;
        if (connection != null) {
            String sql = "UPDATE smbms_user su  SET su.userPassword=?  WHERE su.id=?";
            Object[] params = {password, id};
            execute = BaseDao.execute(connection, preparedStatement, sql, params);
            BaseDao.closeResource(null, null, preparedStatement);
        }
        return execute;
    }
}
4、UserService 业务层接口增加修改用户密码方法
package com.smbms.service.user;

import com.smbms.pojo.User;

public interface UserService {
    //用户登录
    .........
    //修改用户密码
    public  boolean updateUserPassword(int id,String password);
}
5、UserServiceImpl业务层接口实现增加用户密码方法
package com.smbms.service.user;

import com.smbms.dao.BaseDao;
import com.smbms.dao.user.UserDao;
import com.smbms.dao.user.UserDaoImpl;
import com.smbms.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    //业务层调用Dao层,所以要引用Dao层
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

  	//用户登录
		.....
      
    //修改密码
    @Override
    public boolean updateUserPassword(int id, String password) {
        Connection connection=null;
        boolean fiag=false;
        try {
            connection=BaseDao.getConnection();
            if(userDao.updateUserPassword(connection, id, password)>0){
                fiag=true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return  fiag;
    }
}
6、Servlet增加 UserServlet
package com.smbms.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.smbms.pojo.User;
import com.smbms.service.user.UserService;
import com.smbms.service.user.UserServiceImpl;
import com.smbms.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //method 方便Servlet复用
        String method = req.getParameter("method");
        if (method != null && method.equals("savepwd")) {
            this.updatePwd(req, resp);
        }
        else if (method != null && method.equals("pwdmodify")) {
            this.pawModify(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
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        String newpassword = req.getParameter("newpassword");
        boolean flag = false;
        if (user != null && !StringUtils.isNullOrEmpty(newpassword)) {
            UserService service = new UserServiceImpl();
            flag = service.updateUserPassword(user.getId(), newpassword);
            if (flag) {
                req.setAttribute("message", "修改密码成功!!!");
                req.getSession().removeAttribute(Constants.USER_SESSION);
            }
            else {
                req.setAttribute("message", "密码修改失败!!!");
            }
        }
        else {
            req.setAttribute("message", "新密码存在问题!!!");
        }
        try {
            req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //验证旧密码
    public void pawModify(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        String oldpassword = req.getParameter("oldpassword");
        //多功能Map
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if (user==null)
        {
            resultMap.put("result","sessionError");
        }
        else if (StringUtils.isNullOrEmpty(oldpassword)){
            resultMap.put("result","error");
        }
        else {
            String userPaw=user.getUserPassword();
            if(userPaw.equals(oldpassword)){
                resultMap.put("result","true");
            }else {
                resultMap.put("result","false");
            }
        }
        try {
            resp.setContentType("application/json");
            PrintWriter writer = resp.getWriter();
            writer.write(JSONArray.toJSONString(resultMap));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
7、web.xml中注册UserServlet
 <!--用户相关Servlet注册-->
<servlet>
  <servlet-name>UserServlet</servlet-name>
  <servlet-class>com.smbms.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>UserServlet</servlet-name>
  <url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fight。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值