-2

I have the following login codes.

if (uname == "Abigail" && password=="Abby14"){
    response.sendRedirect("http://localhost:8080/Practical_4/member.jsp");
     }

 else {
   response.sendRedirect("http://localhost:8080/Practical_4/index.html");

 }

I realized that my jsp page treats the if-statement as if it's an else statement, and only executes the else-statement.

Jens
  • 67,715
  • 15
  • 98
  • 113
J_fruitty
  • 47
  • 1
  • 1
  • 6

3 Answers3

1

What you do is comparing addresses where strings are stored and not the strings them selfs in some case java will store same string in same address but you cannot count on that.Here is a code example that should explain the issue

public static void main(String... args) {
    String a = "a";
    String b = new String("a");
    String c = "a";
    System.out.println(a==b); // false
    System.out.println(a==c); //true
    System.out.println(a.equals(b)); // true
}

So the buttom line always use equals insterad of ==

urag
  • 1,228
  • 9
  • 28
0

Use equals for string comparison.

 if (uname.equals("Abigail") && password.equals("Abby14")){
        response.sendRedirect("http://localhost:8080/Practical_4/member.jsp");
         }

     else {
       response.sendRedirect("http://localhost:8080/Practical_4/index.html");

     }

Hope this helps.

Subin Chalil
  • 3,531
  • 2
  • 24
  • 38
0

change to use equals and change to order to prevent null pointer

if ("Abigail".equals(uname) && "Abby14".equals(password)) {
Ori Marko
  • 56,308
  • 23
  • 131
  • 233