比较时相同的字符串返回false

时间:2013-06-06 09:35:36

标签: java

有人可以帮助我,为什么比较这两个String返回false 这是代码:

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%        
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    response.setDateHeader("Expires", -1);
%>

<jsp:useBean id="user" class="beans.ConnectToDB" scope="session" />
<jsp:useBean id="aes" class="beans.AES" scope="session" />

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Cache-Control" content="no-cache" />
        <meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT" />
        <title>Authenticating - Checking Credentials</title>
    </head>
    <body>
        <%
            String getUsername = request.getParameter("username");
            String getPassword = request.getParameter("password");  

            final String passphrase = "#asdf@1234#";    
            byte[] password_byte = getPassword.getBytes();  
            byte[] passphrase_byte = passphrase.getBytes();
            byte[] encrypt_password = aes.encrypt(password_byte, passphrase_byte);
            String encrypt_password_str = new String(encrypt_password);     

            if((getUsername != null && !getUsername.isEmpty()) || (getPassword != null && !getPassword.isEmpty())){
                String username_from_db = user.getUsername(getUsername);
                String password_from_db = user.getPassword(getUsername);

                if(getUsername.equalsIgnoreCase(username_from_db) && encrypt_password_str.equals(password_from_db)){
                    response.sendRedirect("client/home_page.jsp");
                }
                else{ 
                    out.println("Original Encrypted Password: " + encrypt_password_str + "<br />");                 
                    out.println("Encrypted Password from D: " + password_from_db);
                }   
            }
            else{ response.sendRedirect("index.jsp"); }
        %>
    </body>
</html>

结果如下:

Original Encrypted Password: I[?y?Ì®õ¹A,®}
Encrypted Password from D: I[?y?Ì®õ¹A,®} 

如果我是正确的,那就是相同的字符串。

1 个答案:

答案 0 :(得分:0)

用户名似乎不一样。打印它们以确保(您只需打印密码)。

if中,其中2个条件与逻辑AND相关联,您确定第二个条件为trueif返回false ,那么第一个条件必须是false