使用JavaServlet从数据库更新密码

时间:2017-12-10 11:30:12

标签: java database jsp servlets nullpointerexception

你好,我正在大学的一个项目工作,我陷入了错误。 我正在处理mypage功能,您可以在其中更新旧密码。问题是ChangepasswordServlet没有按预期执行操作。  

在这里你可以看到我的servlet,问题可能在IF句子的某个地方。 (经理= EntityManager的)

@WebServlet(name = "ChangePasswordServlet", urlPatterns = {"/ChangePassword"})
public class ChangePasswordServlet extends HttpServlet {

@EJB
UserManagerLocal manager;

private void changePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String loggedInUsersUsername = request.getRemoteUser();

    /* PrintWriter writer = response.getWriter(); */


    User userFromTheDB = manager.getUser(loggedInUsersUsername);
    String oldPasswordFromDB = userFromTheDB.getPassword();
    String oldPasswordFromForm = (String) request.getAttribute("theOldPW");
 ;
    String newPasswordFromForm = (String) request.getAttribute("theNewPW");
    String newPasswordToCheckFromForm = (String) request.getAttribute("theNewPWCheck");


    try {
        if (oldPasswordFromDB.toLowerCase().equals(oldPasswordFromForm.toLowerCase()) && newPasswordFromForm.toLowerCase().equals(newPasswordToCheckFromForm.toLowerCase())) {

            userFromTheDB.setPassword(newPasswordFromForm);
            manager.updateUser(userFromTheDB);
            response.sendRedirect("/Slit/MyPage/PasswordSucsess.jsp");

        } else {
            response.sendRedirect("Slit/Error/error.jsp");
        }
    }
    catch (NullPointerException en) {
        PrintWriter print = response.getWriter();
        print.println("nullpointeryes");
        print.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    changePassword(request, response);
}

    @Override
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        changePassword(request, response);
    }  

所以

这是我的JSP,格式为

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
<title>Her kan du endre ditt nåværende passord</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" 
href="/Slit/Templates/CSS/MainPageTemplate.css">
</head>
<body>
<title>Slit</title>
<div>
<div class="form">
    <form method="post" action="/Slit/ChangePassword">
        <input type="Password" name="theOldPW" placeholder="Gammelt 
Password"/>

        <input type="Password" name ="theNewPW" placeholder="Nytt 
Password"/>

        <input type="Password" name ="theNewPWCheck" placeholder=" Nytt 
Password på nytt"/>

    <input type="submit" name="ByttePassord" value="Trykk her for å bytte 
passord!"/>
    </form>
</div>
</div>
</body> 
</html>

</head>
<body>

</body>

所以我在使用旧密码和新密码填写表单后得到nullpointer。 如果我现在正在运行代码,即时获取&#34; nullpointeryes&#34;错误。 编辑: 填写表格后,这是控制台错误:

12:39:37,194 INFO  [stdout] (default task-12) Hibernate: select 
user0_.username as username2_3_0_, user0_.fName as fName3_3_0_, user0_.lName 
as lName4_3_0_, user0_.password as password5_3_0_, user0_.DTYPE as 
DTYPE1_3_0_ from User user0_ where user0_.username=?

感谢您的回答

1 个答案:

答案 0 :(得分:0)

几小时后,我发现我使用了get.attribute而不是参数:/

String newPasswordFromForm = (String) request.getParameter("theNewPW");
String newPasswordToCheckFromForm = (String) 
request.Paramter("theNewPWCheck");