JBuilder2005 and deployment of the actual JSP

Source: Internet
Author: User
Tags exit current time log
User logon and Exit logs

When the user logs on to the system, insert a record in the log table, record the time the user logged in, and log the time the user exits the system when the user exits the system.

We use the Httpsessionbindinglistener interface to record login and exit logs, and two methods are defined in this interface:

Valuebound (Httpsessionbindingevent event)

Valueunbound (Httpsessionbindingevent event)

If a class implements the Httpsessionbindinglistener interface, the object's interface method Valuebound () is invoked automatically when the object is bound to the session through Session.setattribute (). The Valueunbound () method is automatically invoked when an object is moved out of session (by calling Session.invalidate (), Session.removeattribute (), or session automatically expires).

Here we make the User.java class implement the Httpsessionbindinglistener interface, and the adjusted code looks like this:

Code Listing 18 implements the Httpsessionbindinglistener User.java

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 at;
6. Import Java.util.Date;
7.
8. Public class User implements Httpsessionbindinglistener
9. {
10...
Private String logindatetime;//User logon time
12...
public void Valuebound (Httpsessionbindingevent event)
14. {
Connection conn = null;
String sqlstr = "INSERT into T_login_log (ID, user_id, dt_login)" +
"Values" (seq_login_log_id. Nextval,?,? )";
Try
19. {
conn = Dbconnection.getconnection ();
PreparedStatement PStat = conn.preparestatement (SQLSTR);
Logindatetime = Getcurrdatetimestr (); Current Time series
Pstat.setstring (1, userId);
Pstat.setstring (2, logindatetime);
Pstat.executeupdate ();
26.
An. catch (SQLException E)
28. {
throw New RuntimeException (
30. "User login log write error");
Finally
32. {
-Try
34. {
IF (conn!= null)
36. {
Conn.close ();
38.}
catch (SQLException ex)
40. {
Ex.printstacktrace ();
42.}
43.}
44.}
45.
public void Valueunbound (Httpsessionbindingevent event)
47. {
Connection conn = null;
String sqlstr = "Update t_login_log set dt_lonout =?" +
"Where user_id=?" and Dt_login =? ";
Try.
52. {
conn = Dbconnection.getconnection ();
PreparedStatement PStat = conn.preparestatement (SQLSTR);
Pstat.setstring (1, Getcurrdatetimestr ());
Pstat.setstring (2, userId);
Pstat.setstring (3, logindatetime);
Pstat.executeupdate ();
59.
A catch (SQLException e)
61. {
throw New RuntimeException (
63. "User exits log write error");
.} finally
65. {
Try
67. {
IF (conn!= null)
69. {
Conn.close ();
71.}
(SQLException ex)
73. {
Ex.printstacktrace ();
75.}
76.}
77.}
78.
79.//Get the current time string, return in YYYYMMDDHHMMSS format, such as 20050505010101
private static String Getcurrdatetimestr ()
81. {
Simpledate form at SDF = new Simpledate form at ("Yyyymmddhhmmss");
A. return SDF. Form at (new Date ());
84.}
85.}

The Valuebound () method inserts a login log into the T_login_log table, updates the log table's exit time in the Valueunbound () method, and the 80th to 84th line provides a method to get the current time series Getcurrdatetimestr (), This method gets the time string for the logon and exit point in time.

Below is a description of how the program records the user's logon and exit times by describing how the user logged on to the system until exiting:

1. The user enters the password through the login.jsp to log in, the program turns to switch.jsp control page.

2. In switch.jsp, we bind the User.java class object UserBean to the session through the Session.setattribute ("Ses_userbean", UserBean) method.

3. At this point, the Httpsessionbindinglistener interface Method Valuebound () of the Userbean object is invoked, and a login log is inserted into the T_login_log table.

4. Switch.jsp turned to welcome.jsp page.

5. When the user clicks the link in the welcome.jsp page to exit the system, turn to the quit.jsp page.

6. Quit.jsp calls the Session.invalidate () method, and the Userbean object is purged from the session.

7. The Httpsessionbindinglistener interface Method Valueunbound () method of the Userbean object is invoked, the exit time of the log is updated, and the browser window is closed.

The Httpsessionbindinglistener interface is the event interface of the Web container, and the class that implements the interface is invoked automatically when an event occurs, and the Web container has multiple such event interfaces, respectively:

· Servletcontextlistener Interface: The event-handling interface for Web container startup and destruction, with two methods defined in the interface.

· Servletcontextattributelistener Interface: The event-handling interface when a Web context property changes.

· Httpsessionlistener Interface: Session to create and destroy event-handling interfaces for events.

· Httpsessionattributelistener Interface: An event-handling interface that changes the Property object in session sessions, which is similar to the Httpsessionbindinglistener interface we used earlier.

In addition, two additional event-handling interfaces are provided in j2ee1.4, which are:

· Servletrequestlistener Interface: Request requests the object to create and destroy an event-handling interface.

· Servletrequestattributelistener Interface: The event handling interface when changing property objects in request.

Related Article

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.