The Artima Developer Community
Sponsored Link

Java Answers Forum
URGENT: Sessions in JSP

1 reply on 1 page. Most recent reply: Mar 31, 2003 9:23 PM by srinivas raju

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
M?rcio Dorimar

Posts: 1
Nickname: dorimar
Registered: Mar, 2003

URGENT: Sessions in JSP Posted: Mar 28, 2003 12:32 PM
Reply to this message Reply
Advertisement
Hi,
I'm a brazilian man.
Doubt in JSP:
Suppose that I have a database with many users registereds...
How I do for know if a user (registered and logged in yet) is online in the moment that other user search for someone in my database?
Update a field (as "isOnline=yes") in database, after login, is a solution, but how to update the field (for "isOnline=no", by example) when the user session to expires?
I hope you help me.
Sorry my bad english!!!
I'm waiting...


srinivas raju

Posts: 2
Nickname: schekuri
Registered: Mar, 2003

Re: URGENT: Sessions in JSP Posted: Mar 31, 2003 9:23 PM
Reply to this message Reply
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");
}
}
}
}

Flat View: This topic has 1 reply on 1 page
Topic: about showing text,help!!! Previous Topic   Next Topic Topic: inner classes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use