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.… 11. private String loginDatetime; //User login 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.executeUpdate(); 26. 27. } catch (SQLException e) 28. { 29. throw new RuntimeException( 30. "Error writing user login log"); 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.executeUpdate(); 59. 60. } catch (SQLException e) 61. { 62. throw new RuntimeException( 63. "Error writing 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. //Get the current time string and return it 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 login log into the T_LOGIN_LOG table, and updates the exit time of the log table in the valueUnbound() method. In addition, lines 80 to 84 provide a method getCurrDatetimeStr() to obtain the current time string. This method is used to obtain the login and Time string of exit time point.
The following describes how the program records the user's login and logout time by describing the steps that a user goes through until they log out:
1. After the user enters the password to log in through login.jsp, the program switches to the switch.jsp control page.
2. In switch.jsp, we bind the object userBean of the User.java class to the session through the session.setAttribute("ses_userBean", userBean) method.
3. At this time, the HttpSessionBindingListener interface method valueBound() of the userBean object is called and a login log is inserted into the T_LOGIN_LOG table.
4. switch.jsp switches to the welcome.jsp page.
5. When the user clicks the link in the welcome.jsp page to exit the system, he or she is redirected 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 valueUnbound() method of the HttpSessionBindingListener interface method of the userBean object is called, 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. The class that implements the interface is automatically called when an event occurs. The Web container has multiple such event interfaces. They are:
·ServletContextListener interface: event processing interface for web container startup and destruction. Two methods are defined in the interface.
·ServletContextAttributeListener interface: event processing interface when Web context attributes change.
·HttpSessionListener interface: event processing interface for Session creation and destruction events.
·HttpSessionAttributeListener interface: The event processing interface for attribute object changes in the Session session. This interface is similar to the HttpSessionBindingListener interface we used earlier.
In addition, two other event processing interfaces are provided in J2EE1.4, they are:
·ServletRequestListener interface: Request request object creation and destruction event processing interface.
·ServletRequestAttributeListener interface: event processing interface when changing the attribute object in Request.
Program deployment
After the development of the Web program is completed, we start working on program deployment. We hope to deploy this Web application to the Tomcat5.0 Web application server.
First, we set the default homepage of the Web application, and then package the entire Web program into a WAR archive file.
1. Set the default access page, double-click the webModule node in the project pane, JBuilder displays the following page in the content pane:
Figure 26 Set the default page accessed by the web program
Click the Add... button on the right side of the Welcome files list, enter login.jsp in the pop-up dialog box and press the OK button to set the login.jsp page as the default page. In this way, the following bold deployment will be added to the web.xml deployment description file. information:
Code Listing 19 Web application default page
1.…
2. <web-app>
3. <display-name>webModule</display-name>
4. <welcome-file-list>
5. <welcome-file>login.jsp</welcome-file>
6. </welcome-file-list>
7.…
8. </web-app>
When the user does not specify a specific access file name in the URL, the web container automatically checks whether there is a login.jsp file under the URI, and if so, directly calls out this file.
2. Right-click the webModule node in the resource tree in the project pane, Properties...->Build->In the Build settings panel, set Build Web archive to the When building project or module option, as shown in the following figure:
Figure 27 Sets the WAR archive file to be created when Rebuilding the project or Web module
3. Right-click chapter13.jpx in the project pane and select Rebuild in the pop-up menu to compile the entire project.
4. After compilation is completed, a webModule.war file will be generated in the project 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. Next we start the Tomcat 5.0 web application server and access the webModule.war application just deployed.
1. Double-click startup.bat under <JBuilder2005 installation directory>/thirdparty/jakarta-tomcat-5.0.27/bin to start the Tomcat 5.0 Web application server (please ensure that no Web application is running in JBuilder at this time to avoid conflicts).
2. Open IE, type http://localhost:8080/webModule, and you will correctly access the web application just deployed, as shown in the following figure:
Figure 28 Access effect of login.jsp after deployment
The Tomcat server works on port 8080 by default, so you need to add the port number after the machine name. You can change this port number by changing Tomca's server.xml configuration file located in the conf directory.
Since the WAR file of our web application is named webModule.war, after the web server is started, the WAR file will be automatically decompressed to the webModule directory, so it must be accessed through http://localhost:8080/webModule . In addition, since the default access page is login.jsp, when no specific page is specified, the login.jsp page is called.