0

Hello, I am building a servlet which handles the login process after the user clicks the submit button on the previous HTML form. The servlet should get the data entered from the form then perform an SQL statement to select all the data in the registered users table. Once all the data is collected the data from the login form data is validated by comparing it to the data heldin the database. If the comparison fails the user is shown the register.html page to create an account whereas if the comparison produces an exact match the user is shown the home.html page. Now, the trouble I am having is with the comparison part using an if else statement because when I run the servlet by entering a entering incorrect username and passwoed values I am presented with a blank page and the address bar tells me this is on the processlogin servlet so it isn't redirecting me anywhere.

This is the part of my code that is giving me the problems:

try{
	    //String selectSQL = "SELECT * FROM users WHERE username='" + username + "' AND password='" + userpassword + "'";
String selectSQL = "SELECT * FROM users";
						System.err.println("DEBUG: Query: " + selectSQL);
	    Statement stmt = conn.createStatement();
	    //int rows = stmt.executeUpdate(selectSQL);
		 ResultSet rs1 = stmt.executeQuery(selectSQL);
		String validUsername = rs1.getString("username");
		String validPassword = rs1.getString("password");
		 if (validUsername != username && validPassword != userpassword){
		 response.sendRedirect ("../register.html");
}else
{		 
	    	
		Cookie c = new Cookie("status", "vip");
c.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(c);
response.sendRedirect ("../home.html"); //redirect to homepage once user is registered

    }
	conn.close();
	} catch(SQLException se) {
	    System.err.println(se);
	}
	}
	}
	}

Could anyone please shed some light on my issue?

Thank you

PS - If it looks totally wrong it's because I have been trying so many different things to reslove the issue and this is the current state of my code.

2
Contributors
1
Reply
7
Views
7 Years
Discussion Span
Last Post by cbarton.a
0

dsmush,

For String comparisions use the equals() method. For example,

if ( validUsername.equals(username) && validPassword.equals(userpassword) ){	
	 response.sendRedirect ("../register.html");

cbarton.a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.