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:

	    //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");
		Cookie c = new Cookie("status", "vip");
c.setMaxAge(365 * 24 * 60 * 60);
response.sendRedirect ("../home.html"); //redirect to homepage once user is registered

	} catch(SQLException 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.


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

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


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.