How to store the login date and time in JSP for the next login
I am using the following lines of code to output the date onto a browser when the user logs in, so that they see their login date/time.
<%
java.util.Locale locale = request.getLocale();
java.text.DateFormat dateFormat = java.text.DateFormat.getDateTimeInstance( java.text.DateFormat.LONG, java.text.DateFormat.LONG, locale );
= dateFormat.format( new java.util.Date( )) %>
However, what I really want to do is to tell the user their last login time and date (like on this website). How do I store this information for the users next login time?
:confused:
# 1 Re: How to store the login date and time in JSP for the next login
Asa a user login, store time in a DB (update if a row for this user already exist, insert when this is very first login). There is no other way, unless you can be sure that server will never restart(this is impossible though)
Regards,
Mohit
mohit at 2007-11-11 22:39:19 >

# 2 Re: How to store the login date and time in JSP for the next login
You could also set a Cookie for the client on every login, but you then run the risk
of the user having deleted the cookie on the next login. The benefit is that you don't
need any athentication of the client for this method.
sjalle at 2007-11-11 22:40:24 >

# 3 Re: How to store the login date and time in JSP for the next login
I have changed my code and I am using the following lines:
session = request.getSession(true);
.
.
.
session.getLastAccessedTime();
out.println(new java.util.Date(session.getLastAccessedTime()));
I am bit lost, could you kindly tell me how do I store this in a database?
Regards,
Maria
# 4 Re: How to store the login date and time in JSP for the next login
Any ideas how to store session information (such as login time) in a database?
Maria
:WAVE:
# 5 Re: How to store the login date and time in JSP for the next login
You can store it as a java.sql.Date (using a PrepearedStatement to avoid date
formatting problems) or as a long value (in millisec. format), ...
but maybe its the database storing as such that is the problem ?
sjalle at 2007-11-11 22:43:32 >

# 6 Re: How to store the login date and time in JSP for the next login
Wait,
session.getLastAccessedTime();
out.println(new java.util.Date(session.getLastAccessedTime()));
will not solve your requirement. What if user access the site from some other computer. There is no session, no cookie.
See the following steps, I think you are having some problem with DB access, for that you can use PreparedStatement as sjalle suggested.
1) Modify your user table and create a column, last_access of type varchar, make it 255 length.
2) When user logins, get the current server date.
3) Format it in anyway you like.
4) Get the value stored in last_access column and store it in some session variable.
5) Update last_access column with the string you obtained in step 3
6) Display last access date on your pages using session variable you created in step 4
7) If this value is null than this is first time user has log-ined so in this case you may chose not to display last login info or to print some custom message.
Regards,
Mohit
mohit at 2007-11-11 22:44:32 >

# 7 Re: How to store the login date and time in JSP for the next login
If this system utilized authentication (nick & password) a DB solution with
userdata update on every login is the solution. In fact, for
the case of different computers for same user, its the only solution
sjalle at 2007-11-11 22:45:30 >

# 8 Re: How to store the login date and time in JSP for the next login
Thanks guys,
I am using the following code and receiving the message
Welcome Kitania !
You last logged in at: Tuesday 25th October, 2005....
However I am having a problem storing the lastAccessedTime into the database via assigning it to a java.util.Date variable. So I created a variable called newTime and stored the new java.util.Date(session.getLastAccessedTime());into it. However, I am now receiving an error message:
Undefined variable: newTime
String sql1 = "Update insurer set lastAccessedTime = "+ newTime+";";
^
My code is as follows:
String sql = "SELECT * FROM employee WHERE (((employeeID)="+userID+") AND ((password)='"+password+"'));";
Statement stmt =con.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
// lastAccessedTime is an empty variable in the database.
String lastAccessedTime;
String userName;
session.getLastAccessedTime();
userName = rs.getString("fName");
lastAccessedTime = rs.getString("lastAccessedTime");
out.println("<h3>"+ "Welcome " + userName +" "+"!"+ "</h3>");
out.println("You last logged in at: ");
if (lastAccessedTime==null)
{
java.util.Date newTime;
newTime = new java.util.Date(session.getLastAccessedTime());
out.println(new java.util.Date(session.getLastAccessedTime()));
}
else{
out.println(lastAccessedTime);
}
String sql1 = "Update employee set lastAccessedTime = "+ newTime+";";
con = DriverManager.getConnection("jdbc:odbc:laGuardia","","");
Statement stmt1 =con.createStatement();
int rs1 = stmt1.executeUpdate(sql1);
Can you let me know why newTime is being called an undefined variable when I declared and initialized it?
Regards,
Maria
# 9 Re: How to store the login date and time in JSP for the next login
As far as I can see newTime is not declared, at least not in the code you
have posted here (it has to be defined before you use it...)
Anyway, I have never managed to stuff a java.util.Date into an sql database
just like that. I'm using java.sql.Date.
Check the code snippet below, I'm using a prepared statement for updating
a date field in the db and a simple date format to print it (its only an
ms-access db that I use to bang around with, but its sql nevertheless)
import java.sql.*;
import java.text.*;
/**
* Updating a date field in an SQL database
*/
public class MSAccessDB {
public static SimpleDateFormat sd=new SimpleDateFormat("MMM dd yyyy");
private PreparedStatement pStmt=null;
private Statement stmt=null;
private Connection msConn=null;
public MSAccessDB() {
try {
jbInit();
int userID=1;
listLoginData();
updateUserLogin(userID);
listLoginData();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void listLoginData() throws SQLException {
ResultSet rs=stmt.executeQuery("select * from user_table");
while (rs.next()) {
System.out.print(rs.getInt("user_id")+"\t");
System.out.print(rs.getString("nick_name")+"\t");
System.out.print(rs.getString("last_name")+"\t");
System.out.print(rs.getString("first_name")+"\t");
System.out.print(sd.format(rs.getDate("last_access_date"))+"\n");
}
}
private void updateUserLogin(int userID) throws SQLException {
java.sql.Date today=new java.sql.Date(System.currentTimeMillis());
pStmt.setDate(1,today);
pStmt.setInt(2,userID);
pStmt.executeUpdate();
}
private void jbInit() throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
msConn=DriverManager.getConnection("jdbc:odbc:test_db;;;");
String psStr="update user_table set last_access_date=? where user_id=?";
pStmt=msConn.prepareStatement(psStr);
stmt=msConn.createStatement();
}
public static void main(String[] args) {
MSAccessDB mdb=new MSAccessDB();
}
}
sjalle at 2007-11-11 22:47:35 >

# 10 Re: How to store the login date and time in JSP for the next login
Thanks sjalle,
I'll try it.
# 11 Re: How to store the login date and time in JSP for the next login
If you want to put the current time into your DB, why not use a statement like
String sql = "UPDATE user_table SET lastlogin=now() WHERE id=" + userID;
Assuming lastlogin is of type datetime, just let the db get the current date with it's now() function.
Then when you login just retrieve this time along with any other user info and store that in a session DTO.