20201112黄春跃

20201112

今天下午把修改密码写出来了,具体实现:

jsp代码:

修改密码:
<hr />
    <form action="password" method="post" onsubmit="return ver();">
        原 密 码:<input type="password" name="old" value="" required/><br/>
        新 密 码:<input type="password" name="password" id="password" required><br/>
        确认新密码:<input type="password" name="new" id="new" required><br/>
        <b style="color: red" id="error"></b>
        <input type="submit" value="提交" />
        <input type="reset" value="重置" />
    </form>

<script src="<c:url value="/assets/lib/js/jquery.js" />"></script>
<script type="text/javascript">
    function ver() {
        if($("#password").val() == $("#new").val()){
            $("#error").html("");
            return true;
        }
        $("#error").html("两次密码不一致请重新输入");
        return false;
    }
</script>

servlet代码

package com.itlaobing.demo.servlet;

import com.itlaobing.demo.model.UserinfoModel;
import com.itlaobing.demo.util.DBHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Classname PasswordServlet
 * @Description TODO()
 * @Date 2020/11/11 0011 11:58
 * @Author by Administrator
 * @Version v1.0
 */
@WebServlet("/password")
public class PasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("WEB-INF/pages/sys/password.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
              获取密码
              校验两次输入的新密码是否一致
              校验原密码是否正确(可以获取session)
                如果正确,更新数据库数据,更新成功后,跳转登录重新登录
                如果不正确,提示密码不正确
         */
        //获取用户信息
        HttpSession session = req.getSession();
        UserinfoModel model = (UserinfoModel) session.getAttribute("user");
        //获取id
        Integer id =  model.getId();
        //获取用户密码
        String userPassword = model.getPassword();

        //获取原密码
        String oldPassword = req.getParameter("old");

        //获取新密码
        String password = req.getParameter("password");

        //获取确认后的新密码
        String newPassword = req.getParameter("new");

        //如果数据库的密码和原密码相同
        if(userPassword.equals(oldPassword)){
            if(password.equals(newPassword)){
                DBHelper db = new DBHelper();
                String sql = "update userinfo set password = ? where id = ?";
                int row = db.executeUpdate(sql,password,model.getId());
                resp.sendRedirect("login.jsp");
            }else{
                resp.sendRedirect("password.jsp");
            }
        }else{
            resp.sendRedirect("password.jsp");
        }

    }
}

标签

评论

this is is footer