2013-03-29 53 views
0

im問題與.equals一起使用。即時通訊嘗試檢查密碼字符串是否等於數據庫中的密碼值。我在我的serlvet中有密碼字符串,它從表單獲取密碼。它會檢查用戶名罰款密碼.equals和.get一起使用

public boolean acceptCustomer(String username, String password){ 
CustomerTableClass customer = new CustomerTableClass(); 
customer.setCustomerUsername(username); 
customer.setCustomerPassword(password); 
Boolean check = null; 
try 
{ 
entityManager = entityManagerFactory.createEntityManager();    
CustomerTableClass customerTest = new CustomerTableClass(); 
customerTest = entityManager.find(customer.getClass(),username); 
if (customerTest!=null)&&(password.equals(customer.getPassword()))){ 

check= true; 
System.out.println("User found"); 

} 
+0

你檢查了getPassword方法返回的值嗎?因爲我看不到任何錯誤。 – Marco

+0

那麼,是什麼在調試器說的都是password'的'和'customer.getPassword()'值是多少?你做了什麼其他診斷步驟? – millimoose

+0

另外,請格式化您的代碼示例。 – millimoose

回答

1

您有一個額外的支架看起來。試試這個:

(customerTest!=null && password.equals(customer.getPassword())) 
0

您的密碼字段以某種hash可能編碼。你會想湊用戶通過登錄表單上的密碼,然後檢查它,您從數據庫中獲取的密碼。您的數據庫密碼也可能有一個salt。如果它有一個salt,那麼你需要將salt添加到用戶傳入的密碼中,然後將它與數據庫中的密碼進行比較。

0

customer.getPassword()未從數據庫獲取密碼。在使用.equals之前,我必須找到並檢索密碼。它的工作完美現在繼承人代碼

public boolean acceptCustomer(String username, String password){ 
    CustomerTableClass customer = new CustomerTableClass(); 
    customer.setCustomerUsername(username); 
    customer.setCustomerPassword(password); 
    Boolean check = null; 

    try 
    { 
     entityManager = entityManagerFactory.createEntityManager();    
     CustomerTableClass customerTest = new CustomerTableClass(); 
     customerTest = entityManager.find(customer.getClass(),username);// .find checks if username aleady exists 
     if (customerTest!=null){// if user exists 
      customer = entityManager.find(customer.getClass(), username);// retrieve user from db 
      customer.getPassword();//retrieve password from db 
      if (password.equals(customer.getPassword()))//if they are equal 
      { 
       System.out.println("user and password correct"); 
       check= true;  
      } 
      else 
      { 
       System.out.println("user and password not correct"); 
       check= false; 
       entityManager.close(); 
      } 
     } 
     else 
     { 
      System.out.print("user does not exist"); 
     } 
    } 
+0

如果您要關閉的EntityManager我想你會想這樣做,在你嘗試{}塊和底部可能在finally {}子句 – Jazzepi