Servlet listening event

Source: Internet
Author: User
Httpsessionbindinglistener interface is the Web Container event interface. The classes that implement the interface are automatically called when an event occurs. The Web Container has multiple such event interfaces:

· Servletcontextlistener interface: The event processing interface for Web Container startup and destruction. The interface defines two methods.

· Servletcontextattributelistener interface: The event processing interface when the web context attribute is changed.

· Httpsessionlistener interface: The event processing interface for session creation and destruction.

· Httpsessionattributelistener interface: The event processing interface for modifying attribute objects in session. This interface is similar to the httpsessionbindinglistener interface we used earlier.

In addition, j2ee1.4 provides two other event processing interfaces:

· Servletrequestlistener interface: interface for creating and destroying request objects.

· Servletrequestattributelistener interface: The event processing interface used to change attribute objects in a request.

Injection listening method:

Add in Web. xml

<Listener>
<Listener-class> com. First. ticss. dataoperator. ticsssessionmanager </listener-class>
</Listener>

Note: classes that implement the httpsessionbindinglistener interface do not need to be set in Web. xml!

Instance: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
34 .{
35. If (Conn! = NULL)
36 .{
37. Conn. Close ();
38 .}
39.} catch (sqlexception ex)
40 .{
41. ex. printstacktrace ();
42 .}
43 .}
44 .}
45.
46. Public void valueunbound (httpsessionbindingevent event)
47 .{
48. Connection conn = NULL;
49. String sqlstr = "Update t_login_log set dt_lonout =? "+
50. "Where user_id =? And dt_login =? ";
51. Try
52 .{
53. Conn = dbconnection. getconnection ();
54. preparedstatement pstat = conn. preparestatement (sqlstr );
55. pstat. setstring (1, getcurrdatetimestr ());
56. pstat. setstring (2, userid );
57. pstat. setstring (3, logindatetime );
58. pstat.exe cuteupdate ();
59.
60.} catch (sqlexception E)
61 .{
62. Throw new runtimeexception (
63. "An error occurred while writing the user exit log ");
64.} finally
65 .{
66. Try
67 .{
68. If (Conn! = NULL)
69 .{
70. Conn. Close ();
71 .}
72.} catch (sqlexception ex)
73 .{
74. ex. printstacktrace ();
75 .}
76 .}
77 .}
78.
79. // obtain the current time string, which is returned in yyyymmddhhmmss format, such as 20050505010101
80. Private Static string getcurrdatetimestr ()
81 .{
82. simpledate form at SDF = new simpledate form at ("yyyymmddhhmmss ");
83. Return SDF. Form at (new date ());
84 .}

85 .}

The valuebound () method inserts a log into the t_login_log table, and updates the log table exit time in the valueunbound () method ~ Row 84 provides a method to get the current time string getcurrdatetimestr (). This method is used to obtain the time string of the logon and exit time points.

The following describes how the program records the user's logon time and exit time when the user logs on to the system until the user exits:

1. After the user enters the password through login. jsp, the program switches to the switch. jsp control page.

2. In switch. jsp, we use the session. setattribute ("ses_userbean", userbean) method to bind the userbean object of the user. Java class to the session.

3. At this time, the userbean object's httpsessionbindinglistener interface method valuebound () is called and a log is inserted into the t_login_log table.

4. Switch. jsp switch to the welcome. jsp page.

5. When you click the link on the welcome. jsp page to exit the system, you can switch to the quit. jsp page.

6. Quit. jsp calls the session. invalidate () method, and the userbean object is cleared from the session.

7. At this time, the userbean object's httpsessionbindinglistener interface method valueunbound () method is called, the log exit time is updated, and the browser window is closed.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.