Servlet does not receive the URL parameter

Asked

Viewed 57 times

0

I’m trying to create a basic password recovery system. Each user has a token stored in the database and when trying to create a new password, it would be stored in the same account as the token value shown in the URL.

Example, I have this URL: http://localhost:8080/login/reset-password.jsp? token=cf76955642884215ab05ddfe20852aed with the token specified as parameter.

The right one would be to save the password where the token had this value: "cf76955642884215ab05ddfe20852aed" but when I try to enter a new password, I get the message that the password has been changed, but nothing changes in the database.

I think it is some problem with the request of the parameter by URL in Servlet, because when I enter this value directly in Sql query in reset_token, it works perfectly.

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String password = request.getParameter("password");
        String token = request.getParameter("token");



                ForgotPasswordHandler.UpdatePassword(password, token);

                String message = "Password changed!";
                request.setAttribute("message", message);
                request.getRequestDispatcher("reset-password.jsp?token=" + token).forward(request, response);   



    } 

And the Forgotpasswordhandler.java class method that updates the password value in the database:

public static void UpdatePassword(String password, String token) throws SQLException {

            PreparedStatement ps = null;
            try
            {
                if (con == null){
                    System.out.println("Failed connection");
                } else {
                    ps = con.prepareStatement(
                        "UPDATE user SET password = ? WHERE reset_token = ?");

                    ps.setString(1,password);
                    ps.setString(2,token);

                    ps.executeUpdate();   

                   if (!con.getAutoCommit()) {
                      con.commit();
                   }
                }
            }
            catch (Exception e) {
                    e.printStackTrace(System.out);    
            } 
            finally {
                if (ps != null) {
                    ps.close();
                }
            }
  • I think the problem is that a POST request takes the parameters of the body (body), not the URL. Try passing the token in the body of the request.

  • @Piovezan Caramba, thank you very much! I ended up inserting the URL parameter in a Hidden input and it worked! Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.