0

Whenever i click on submit it redirects to a blank page but the url works fine im confused on whether my response tag is wrong

Code:

protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {             
    Connection con=connector.getConnection();
    String username = request.getParameter("username");  
    String password = request.getParameter("password");
    String message = null;
    String url = null;
   try { 
        PreparedStatement ps = con.prepareStatement( "Select * from data Where username=?'" + username + "' and password=?'" + password + "';");
        ps.setString(1, request.getParameter("username"));
        ps.setString(2, request.getParameter("password"));
        ResultSet rs = ps.executeQuery();
               if(rs.next()) {
               RequestDispatcher rd=request.getRequestDispatcher("practice1.jsp");
               rd.forward(request,response);

               }
               else {
               message="Invalid Input Please Try Again";
             request.setAttribute("message", message);
             request.setAttribute("url", url);

           }
   }


   catch(SQLException ex){
    message="ERROR"+ex.getMessage();
    ex.printStackTrace();
}

Please let me know What i am missing.

Amol Bais
  • 332
  • 1
  • 6
  • 30
  • You are not forwarding your request anywhere if the code executes the `else` statement. That would give you a blank page. – DanielBarbarian Oct 14 '19 at 06:21
  • i even tried using while and erasing if else – Just a New Programmer Oct 14 '19 at 06:26
  • The query does not seem correct. I don't think you meant to pass the values of the _username_ and _password_ in the prepared statement query string; you just need to set the parameters (as you do right after). Given this, I believe that your code throws an exception, which is caught in the _catch_ clause, meaning that you are not redirecting. Check your log files for the exception. – George Alyfantis Oct 14 '19 at 06:47

2 Answers2

1

Try to fix it, by changing below code:

Before:

PreparedStatement ps = con.prepareStatement( "Select * from data Where username=?'" + username + "' and password=?'" + password + "';");
ps.setString(1, request.getParameter("username"));
ps.setString(2, request.getParameter("password"));

After:

PreparedStatement ps = con.prepareStatement( "Select * from data Where username=? and password=?");
ps.setString(1, username);
ps.setString(2, password);
Amol Bais
  • 332
  • 1
  • 6
  • 30
wacame
  • 26
  • 2
0

In the if block whenever there are data the request dispatcher will forward the request to practice1.jsp page.

if(rs.next()) {
               RequestDispatcher rd=request.getRequestDispatcher("practice1.jsp");
               rd.forward(request,response);
               }

But in case of else block only request are being set as follows:

request.setAttribute("message", message);
request.setAttribute("url", url);

But we should specify the response not request if we want to display in UI.

else {
         message="Invalid Input Please Try Again";
         response.getWriter().print("<html><head><title>
                                 Oops an error happened!</title> </head>");
         response.getWriter().print("<body>"+message+"</body>");
         response.getWriter().println("</html>");
         //request.setAttribute("message", message);
         //request.setAttribute("url", url);

         }