|
Re: URGENT: Sessions in JSP
|
Posted: Mar 31, 2003 9:23 PM
|
|
hai u brazilian,
i know only of ur football players and Rio carnival about brazil.
u should implement HttpSessionBindingListner class and here is the code
public class LogoutUpdate implements HttpSessionBindingListener, Serializable {
/** * No-arg constructor */ public LogoutUpdate() { super(); }
/** * The method <code>valueBound</code> is called by the container when the session is Activated. * @param: e HttpSessionBindingEvent */ public void valueBound(HttpSessionBindingEvent e) { Logger.logEvent("Logout Object bound to Session"); } /** * The method <code>valueUnbound</code> is called by the container when the session is Deactivated. * @param: e HttpSessionBindingEvent * * THIS METHOD IS NOT USED CURRENTLY. * IN THE RELEASE OF TOMCAT4.1, WHEN valueUnbound(e) CALLED, THE ATTRIBUTE "loginhistid" MAY ALREADY * HAS BEEN REMOVED. WE NEED TO CHANGE CODE OF expire() and removeAttribute(). * FOR NOW, A NEW METHOD unboundUser(loginhistid) IS CALLED WHENEVER USER CLICKS ON "LOGOUT" BUTTON. */
public void valueUnbound(HttpSessionBindingEvent e) { /* DbConnection dbc = new DbConnection(); Connection connection = dbc.getConnection(); Statement stmt = null; ResultSet res = null; int updateCount = -1; //int id = -1; Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DATE); int month = cal.get(Calendar.MONTH) + 1; int year = cal.get(Calendar.YEAR); int hour = cal.get(Calendar.HOUR); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); int ampm = cal.get(Calendar.AM_PM); String sDay = Integer.toString(cal.get(Calendar.DATE)); String sMonth = Integer.toString(cal.get(Calendar.MONTH) + 1); String sYear = Integer.toString(cal.get(Calendar.YEAR)); String sHour = Integer.toString(cal.get(Calendar.HOUR)); String sMinute = Integer.toString(cal.get(Calendar.MINUTE)); String sSecond = Integer.toString(cal.get(Calendar.SECOND)); String sAmpm = "AM"; if (ampm == 1) { sAmpm = "PM"; } String currentDate = sMonth + "-" + sDay + "-" + sYear + " " + sHour + ":" + sMinute + ":" + sSecond + " " + sAmpm; String sLogoutTime = FormatDate.getDBFormatDate(currentDate, FormatDate.MM_DD_YYYY_HHMMSS_AMPM); try { HttpSession session = e.getSession(); Integer iLoginHistId = (Integer) session.getValue("loginhistid"); String updateStmt = "UPDATE login_history SET " + " logout_time = " + "'" + sLogoutTime + "'" + " where id = " + iLoginHistId + " and login_status = 1"; Logger.logEvent("common.LogoutUpdate.valueUnbound : Logout update query:" + updateStmt); stmt = connection.createStatement(); updateCount = stmt.executeUpdate(updateStmt); if (updateCount == 1) { Logger.logEvent("common.LogoutUpdate.valueUnbound : Updated successfully..."); // setErrorFlag(AppExceptions.SUCCESSFUL); } else { Logger.logEvent("common.LogoutUpdate.valueUnbound : Could not update logout time into the database..."); // setErrorFlag(AppExceptions.FAILURE ); // setErrorMsg("Error while updating logout status of ID " + iLoginHistId +"."); } } //end of try block catch (SQLException se) { Logger.logEvent("common.LogoutUpdate.valueUnbound : SQLErrorState = " + se.getSQLState()); } catch (Exception ee) { Logger.logEvent("common.LogoutUpdate.valueUnbound : Exception has occurred " + ee.getMessage()); // setErrorFlag(AppExceptions.FAILURE ); // setErrorMsg("System errors occurred, please contact your system administrator for more information."); } finally { try { if (stmt != null) { stmt.close(); stmt = null; } if (connection != null) { connection.close(); connection = null; } dbc = null; } catch (Exception ex) { Logger.logEvent("common.LogoutUpdate.valueUnbound : Exception closing statement/connection"); } } Logger.logEvent("Logout Object is unbound from Session"); */ }
/** * The method <code>valueUnbound</code> is called by the container when the session is Deactivated. * @param: e HttpSessionBindingEvent * THIS METHOD IS REPLACEMENT FOR valueUnbound(event) METHOD. */ public void unboundUser(int loginhistId) { UiTools ut = new UiTools(); DbConnection dbc = new DbConnection(); Connection connection = dbc.getConnection(); Statement stmt = null; ResultSet res = null; int updateCount = -1; //int id = -1; Calendar cal = Calendar.getInstance(); /* int day = cal.get(Calendar.DATE); int month = cal.get(Calendar.MONTH) + 1; int year = cal.get(Calendar.YEAR); int hour = cal.get(Calendar.HOUR); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); */ int ampm = cal.get(Calendar.AM_PM); String sDay = Integer.toString(cal.get(Calendar.DATE)); String sMonth = Integer.toString(cal.get(Calendar.MONTH) + 1); String sYear = Integer.toString(cal.get(Calendar.YEAR)); String sHour = Integer.toString(cal.get(Calendar.HOUR)); String sMinute = Integer.toString(cal.get(Calendar.MINUTE)); String sSecond = Integer.toString(cal.get(Calendar.SECOND)); String sAmpm = "AM"; if (ampm == 1) { sAmpm = "PM"; } String currentDate = sMonth + "-" + sDay + "-" + sYear + " " + sHour + ":" + sMinute + ":" + sSecond + " " + sAmpm; String sLogoutTime = FormatDate.getDBFormatDate(currentDate, FormatDate.MM_DD_YYYY_HHMMSS_AMPM);
try { int iLoginHistId = loginhistId; String updateStmt = "UPDATE login_history SET " + " logout_time = '" + sLogoutTime + "' where id = " + iLoginHistId + " and login_status = 1"; Logger.logEvent("common.LogoutUpdate.unboundUser : Logout update query:" + updateStmt); stmt = connection.createStatement(); updateCount = stmt.executeUpdate(updateStmt); if (updateCount == 1) { Logger.logEvent("common.LogoutUpdate.unboundUser : Updated successfully..."); // setErrorFlag(AppExceptions.SUCCESSFUL); } else { Logger.logEvent("common.LogoutUpdate.unboundUser : Could not update logout time into the database..."); // setErrorFlag(AppExceptions.FAILURE ); // setErrorMsg("Error while updating logout status of ID " + iLoginHistId +"."); } } //end of try block catch (SQLException se) { Logger.logEvent("common.LogoutUpdate.unboundUser : SQLErrorState = " + se.getSQLState()); } catch (Exception ee) { Logger.logEvent("common.LogoutUpdate.unboundUser : Exception has occurred " + ee.getMessage()); // setErrorFlag(AppExceptions.FAILURE ); // setErrorMsg("System errors occurred, please contact your system administrator for more information."); } finally { try { if (stmt != null) { stmt.close(); stmt = null; } if (connection != null) { connection.close(); connection = null; } dbc = null; } catch (Exception ex) { Logger.logEvent("common.LogoutUpdate.unboundUser : Exception closing statement/connection"); } } } }
|
|