Home > Articles > Security > General Security and Privacy

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Resetting the Login Attempt Count

The final requirement is to reset the failed login attempt count upon successful login. Again, this requires an additional private method to keep the login method as clean as possible:

 private void resetCounter() {
  String username = getUsername();
  Connection con = null;
  try {
   InitialContext ic = new InitialContext();
   DataSource ds = (DataSource) ic.lookup(dsJndiName);
   con = ds.getConnection();
   PreparedStatement ps = con.prepareStatement(resetCounterQuery);
   ps.setString(1, username);
  } catch (NamingException e) {
   _log.error("Unexpected error", e);
  } catch (SQLException e) {
   _log.error("Query failed", e);
  } finally {
   try {
    if (con != null) {
   } catch (Throwable e) {
    _log.error("Error closing connection", e);

This method is also called just prior to returning true at the bottom of the login method. This addition to the login method will also be shown at the end of this article in the completed classes source code.

  • + Share This
  • 🔖 Save To Your Account