0

I am trying to make a login system in java by using the result that a page gives me when getting the page's content over java like when i get the data from

    http://localhost/java.php?un=dubking&password=password

when the login credentials are correct that page will only show "true" as html but when i use the array that has the data from that page wich actually has "true" in it in an if statement the if statement doesn't work, is there something I did wrong or is this just not possible in Java?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
class test2 {
    public void calc() {
        try { 
            URL yahoo = new URL("http://localhost/java.php?un=" + username + "&password=" + password); 
            BufferedReader in = new BufferedReader( 
                    new InputStreamReader(yahoo.openStream())); 
            String inputLine; 

            while ((inputLine = in.readLine()) != null) { 
                // Process each line.
                String html = inputLine;
                if (html == "true") {
                    System.out.print("You have successfully logged in.");
                } else {
                    if (html == "false") {
                        System.out.println("Wrong username or password.");
                    }
                    if (html != "true" && html != "false") {

                        System.out.println("something went wrong.");
                        System.out.println(html);
                    }
                }
            } 
            in.close(); 

        } catch (MalformedURLException me) { 
            System.out.println(me); 

        } catch (IOException ioe) { 
            System.out.println(ioe); 
        } 

    }
}
beatgammit
  • 19,817
  • 19
  • 86
  • 129
Sam Amant
  • 19
  • 3

2 Answers2

4

Use String#equals to compare String contents. The == operator compares Object references. The String returned from BufferedReader#readLine will be a different object from any interned Strings used in your application (e.g. "true", "false").

Better still use equalsIgnoreCase to handle any variations in case. In addition a Joda condition will protect against a NullPointerException should html be null

if ("true".equalsIgnoreCase(html)) {
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

You are trying to check string equality using == and != and that will never work. You need to change == to .equals instead. Like this

"true".equals(html)

Or

!"true".equals(html)
cmbaxter
  • 35,283
  • 4
  • 86
  • 95