Reputation:
I am creating an appliaction which requires user authentication. I have one problem when I'm trying to log in. When I type a correct username and password, the onSuccess method is called. But when I type a wrong one, or empty fields, then the onFailure() method is NOT called.
I really want to know why this is happening. Because I wan't to display some sort of dialogbox when the username or password is incorrect.
This is the ClickHandler, which takes the username and password from the fields:
loginButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String username = usernameBox.getText();
String password = passwordBox.getText();
performUserConnection(username, password);
}
});
And this is the method that performs the user conenction, which as I said, works if I have a correct username/ password. It displays my alert message, but it does not display any alert message if it's not correct.
private static void performUserConnection(String username, String password) {
DBConnectionAsync rpcService = (DBConnectionAsync) GWT.create(DBConnection.class);
ServiceDefTarget target = (ServiceDefTarget) rpcService;
String moduleRelativeURL = GWT.getModuleBaseURL() + "DBConnectionImpl";
target.setServiceEntryPoint(moduleRelativeURL);
rpcService.authenticateUser(username, password, new AsyncCallback<User>() {
@Override
public void onSuccess(User user) {
Window.alert("TRALALA. Username: " + user.getUsername());
}
@Override
public void onFailure(Throwable caught) {
Window.alert("LALALALAL");
// DialogBox dialogBox = createDialogBox();
// dialogBox.setGlassEnabled(true);
// dialogBox.setAnimationEnabled(true);
// dialogBox.center();
// dialogBox.show();
}
});
}
UPDATE Server Part.
public class DBConnectionImpl extends RemoteServiceServlet implements DBConnection {
private static final long serialVersionUID = 1L;
private String URL = new String("jdbc:mysql://localhost:3306");
private String user = "root";
private String pass = "andrei";
private String schema = "administrare_bloc";
public DBConnectionImpl() {
}
private Connection getConnection() throws Exception {
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", pass);
props.setProperty("zeroDateTimeBehavior", "convertToNull");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(URL + "/" + schema, props);
return conn;
}
@Override
public User authenticateUser(String username, String password) throws Exception {
User returnUser = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
try {
String q = "select * from users where username=? and password=?";
stmt = conn.prepareStatement(q);
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String user = rs.getString("username");
String pass = rs.getString("password");
String type = rs.getString("type");
returnUser = new User(id, user, pass, type);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
rs.close();
stmt.close();
conn.close();
}
return returnUser;
}
}
Thanks in advance
Upvotes: 1
Views: 235
Reputation: 5208
The onFailure method will only be Called if you throw an exception on the server. Now you just return a null object if no user is found.
Upvotes: 4