Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, im trying to code forgot password link, below is the code that i have done but the problem is it still send me to Error page instead of the Pasword page even if the email entered is correct....please assist im new to java

What I have tried:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email = request.getParameter("email");
        
        PrintWriter out = response.getWriter();
        boolean isEmailValid=false;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "root");
            PreparedStatement pst = conn.prepareStatement("Select user_email from users where user_email=? ");
            pst.setString(1, email);
          
            ResultSet rs = pst.executeQuery();
            HttpSession session = request.getSession(false);
           
            if (rs.next()) {
                if (rs.getString("user_email").equals(email)) {
                    isEmailValid=true;
                                     }
                            }if(isEmailValid){
                session.setAttribute("Email", email);
                response.sendRedirect("Password.jsp");
            
            }else 
              response.sendRedirect("ErrorPage.jsp");

            

        } catch (ClassNotFoundException | SQLException e) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
        }

    }
Posted
Comments
[no name] 16-Feb-17 9:33am    
Should be easy enough to debug. Looks like your query is not returning any results.
Richard Deeming 16-Feb-17 11:38am    
That looks like a very insecure implementation. You're allowing anyone to reset the password by simply guessing the email address of a registered user!

Have a look at Troy Hunt's excellent blog post[^], which covers a lot of the things you need to consider when building a secure password reset facility.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900