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.
Program Deployment
After the Web program was developed, we started working on the program deployment, and we wanted to deploy the Web application to the Tomcat5.0 Web application server.
First, we set the default homepage for the Web application and then make the entire Web program a war archive package.
1. Set the default Access page, double-click the Webmodule node in the Engineering pane, and jbuilder the following page in the content pane:
Figure 26 Setting the Web program's default access page
Click the Add on the right of the Welcome files list ... button, enter the login.jsp in the pop-up dialog box and press the OK button to use the Login.jsp page as the default page, so the following bold deployment information will be added to the Web.xml deployment description file:
Code listing Web application default page
1...
2. The <web-app>
3. <display-name> Webmodule </display-name>
4. The <welcome-file-list>
5. <welcome-file> login.jsp </welcome-file>
6. The </welcome-file-list>
7...
8. The </web-app>
When the user does not specify a specific access file name in the URL, the Web container automatically looks at the login.jsp file under the URI, if the file is directly paged.
2. Right-click the Webmodule node in the resource tree in the Engineering pane, properties...->build-> the build Web archive to the When building the project or module option in the build setup plane. As shown in the following illustration:
Figure 27 Setting up a war profile when rebuild a project or Web module
3. Right-click CHAPTER13.JPX in the Engineering pane and select Rebuild in the pop-up menu to compile the entire project.
4. When the compilation is complete, a Webmodule.war file will be generated in the engineering root directory.
5. Copy the Webmodule.war file to the <jbuilder2005 installation directory >/thirdparty/jakarta-tomcat-5.0.27/webapps directory.
This completes the deployment of the Web application, and we start the Tomcat 5.0 Web application Server and access the Webmodule.war application that we just deployed.
1. Double-click the Startup.bat boot Tomcat 5.0 under the <jbuilder2005 installation directory >/thirdparty/jakarta-tomcat-5.0.27/bin Web Application Server (make sure that the Web application is not running in JBuilder to avoid conflicts).
2. Open IE and type http://localhost:8080/webModule to access the Web application you just deployed, as shown in the following illustration:
Figure 28 The results of the login.jsp after the department
The TOMCAT server is working on port 8080 by default, so you need to add a port number after the machine name, and you can change this port number by changing the Server.xml profile located in the Conf directory TOMCA.
Since our Web application's War file name is Webmodule.war,web server startup, the war file is automatically extracted into the Webmodule directory and must be accessed through Http://localhost:8080/webModule. In addition, because the default access page is login.jsp, the login.jsp page is called for Access when no specific page is specified.
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.