简体   繁体   中英

JSP Login Logout simple web application using bean - returning false when data is present in DB

I am learning J2EE, tried to make we application - a simple loginLogout application Using JSAP and Beans.

 db data is:
 name     email             pass
 rita     rita@123          rita@567
 scdjkc   abc@gmail.com     12376
 jsgdjj   abcdef@gmail.com  123476
 sgApurva abcdef@gmail.com  123476
 sgApurva abc123@gmail.com  123e3476

when i am giving "rita@123" in email and "rita@567" in pass it says: incorrect credentials- it means return value is false.I am not able to understand as to why is it returning false when that data is already present in db. when i tried hard coding these values it is properly showing correct message.

I wrote the same thing in java application then this is returning true. Please help me resolve this issue.
Following is my code for db connection and fetching data:

//String query="select * from emp2 where email='rita@123' and pass='rita@567'";
String query= "select * from emp2 where email=? and pass=?";
try{  
    Connection con = ConnectionProvider.getCon();  

    if(con==null){
        return "value";
    }

    PreparedStatement ps=con.prepareStatement(query);  
    ps.setString(1,bean.getEmail());  
    ps.setString(2,bean.getPass());  
    ResultSet rs = ps.executeQuery();  

    if(rs.next()){
        return "true";
    }
    else{
        return "false";
    } 

}catch(Exception e){
    e.printStackTrace();
}  

return "error";  

Is it possible you have spaces `? If the hard coded value is working this mean that the incoming string is different. Try debuging and see what is comming. You can also place assertions for what you expect to be coming as input and log what it realy is.

您可以使用trim()方法消除字符串上可能的空格:

bean.getEmail().trim();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM