User logon and exit logs
When a user logs on to the system, insert a record in the log table to record the logon time of the user, and record the time when the user exits the system.
We use the HttpSessionBindingListener interface to record logon and exit logs. This interface defines two methods:
· ValueBound (HttpSessionBindingEvent event)
· ValueUnbound (HttpSessionBindingEvent event)
If a class implements the HttpSessionBindingListener interface, when the object passes session. when setAttribute () is bound to a Session, the interface method valueBound () of the object is automatically called. When the object is removed from the session (by calling the session. invalidate (), session. when removeAttribute () or session automatically expires, the valueUnbound () method is automatically called.
The following code uses the User. java class to implement the HttpSessionBindingListener interface. The adjusted code is as follows:
Code List 18 implements User. java of HttpSessionBindingListener
1. package bookstore;
2. import javax. servlet. http. HttpSessionBindingListener;
3. import javax. servlet. http. HttpSessionBindingEvent;
4. import java. SQL .*;
5. import java. text. SimpleDate form;
6. import java. util. Date;
7.
8. public class User implements HttpSessionBindingListener
9 .{
10 ....
11. private String loginDatetime; // user logon time
12 ....
13. public void valueBound (HttpSessionBindingEvent event)
14 .{
15. Connection conn = null;
16. String sqlStr = "insert into T_LOGIN_LOG (ID, USER_ID, DT_LOGIN)" +
17. "values (SEQ_LOGIN_LOG_ID.NEXTVAL ,?,? )";
18. try
19 .{
20. conn = DBConnection. getConnection ();
21. PreparedStatement pStat = conn. prepareStatement (sqlStr );
22. loginDatetime = getCurrDatetimeStr (); // Current Time String
23. pStat. setString (1, userId );
24. pStat. setString (2, loginDatetime );
25. pStat.exe cuteUpdate ();
26.
27.} catch (SQLException e)
28 .{
29. throw new RuntimeException (
30. "An error occurred while writing user login logs ");
31.} finally
32 .{
33. try
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.