I am studying the following to implement 'Remember me" functionality : http://varuntayur.wordpress.com/2012/01/25/session-management-in-gwt/
I have most of it working however, and my apologies this is probably very basic, I am having difficulty returning the session ID to the client side.
My server side code is:
The call to the DB to verify user and return details:
public Account authenticateAccount(String accountName, String pass) {
Account account = null; // necessary unless you do something in the exception handler
String stored_hash = null;
PreparedStatement ps = null;
// Create connection/statement variables outside of try block
Connection c = null;
String selectQry = ("SELECT acc_id, grp_id, acc_password, acc_level, acc_enabled, acc_archived " +
"FROM at_accounts " +
"WHERE acc_email_address = ?;");
try {
// Get Connection and Statement from DataSource
c = ds.getConnection();
ps = c.prepareStatement(selectQry);
try {
// Create a statement and execute the query on it
ps.setString(1, accountName);
// Get result set
ResultSet result = ps.executeQuery();
while (result.next()) {
account = new Account(result.getString(1), result.getString(2), null, result.getString(3),
result.getString(4), null, result.getInt(5), result.getDate(6), null);
stored_hash = result.getString(3);
}
// Clean up
ps.close();
c.close();
} catch (SQLException se) {
System.out.println("SQLException occurred in authenticateAccount: " + se.toString());
} catch (Exception e) {
System.out.println("Errors occurred in authenticateAccount: " + e.toString());
}
} catch (SQLException e1) {
System.out.println("SQLException occurred in authenticateAccount: " + e1.toString());
e1.printStackTrace();
} finally {
// Ensure connection is closed and returned to the pool, even if errors occur.
// This is *very* important if using a connection pool, because after all the
// connections are used, the application will hang on getConnection(), waiting
// for a connection to become available.
// Any errors from the following closes are just ignored. The main thing is
// that we have definitely closed the connection.
try { if(ps != null) ps.close(); } catch (Exception e) {}
try { if(c != null) c.close(); } catch (Exception e) {}
}
// Done
//Check that the hashed value of the password equals the stored hashed value
//If it does not then account will be set to null.
if (stored_hash != null) {
if (BCrypt.checkpw(pass, stored_hash)) {
} else {
account = null;
}
} else {
account = null;
}
storeUserInSession(account);
return account;
}
The code to store the session ID
private void storeUserInSession(Account account)
{
HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
HttpSession session = httpServletRequest.getSession(true);
session.setAttribute("account", account);
System.out.println("storeUserInSession: " + account);
}
The println returns: "storeUserInSession: org.AwardTracker.client.Account@2eca7997".
When this is returned to the client side I use the following code to retrieve the session ID and create the cookie:
//Remember the Login
String sessionID = account.getSessionId();
Window.alert("Session ID = " + sessionID);
final long DURATION = 1000 * 60 * 60 * 24 * 14; //duration remembering login. 2 weeks in this example.
Date expires = new Date(System.currentTimeMillis() + DURATION);
Cookies.setCookie("sid", sessionID, expires, null, "/", false);
The Window.alert shows account.getSessionId() to be null (all other account information is returned correctly, e.g., access level (user, admin, etc)).
Thanks for any assistance.
Regards,
Glyn