我的JSP页面仅执行其他语句



我有以下登录代码。

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

我意识到我的JSP页面将if-statement视为另一个语句,并且仅执行其他说明。

您要做的是比较存储字符串的地址,而不是字符串自self在某种程度上,Java将同一字符串存储在同一地址中,但您不能指望那个。应该解释这个问题

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
}

因此,屁股线总是使用==

的insterad

使用equals进行字符串比较。

 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");
     }

希望这会有所帮助。

更改以使用平等和更改以防止Null指针

if ("Abigail".equals(uname) && "Abby14".equals(password)) {

最新更新