Welcome page welcome.jsp
When the user enters the correct password, switch.jsp controls the JSP page to direct the welcome.jsp welcome page. In this section, we develop the welcome.jsp page. Because welcome.jsp needs to use a third-party tag library, you need to make some configurations in JBuilder to introduce this tag library before developing welcome.jsp.
Configure third-party tag libraries into JBuilder
The Apache open source organization provides many useful tag libraries. The welcome page welcome.jsp needs to use a datetime tag library from Apache. You can use this tag library to provide various time displays in JSP. The download address is: /u/info_img /2009-07/02/jakarta-taglibs-datetime-1.0.1.zip.
The tag library generally includes two files, one is the class package JAR file, and the other is the tag description file with the extension .tld. After decompressing the downloaded compressed file, we place the taglibs-datetime.jar and taglibs-datetime.tld files in the <project directory>/datetimeTag directory.
To use a third-party tag library in a project, you must configure the tag library in JBuilder in advance and reference it in the project. Configuring tag libraries is similar to configuring class libraries, and is also done through the Configure Libraries dialog box. The JBuilder class library and tag library are listed in the tree on the left in the Configure Libraries dialog box. The class library is displayed as icon, while the tag library appears as icon. Next we configure the datetime tag library into JBuilder.
1. Tools->Configure->Libraries->Configure Libraries dialog box.
Click the Add... button in the lower left corner of the Configure Libraries dialog box to pop up the New Library Wizard dialog box, as shown below:
Figure 17 New Library Wizard dialog box |
Give this library a name in Name: datetimeTag, and press OK to return to the Configure Libraries dialog box.
2. Specify the tag library file.
After returning to the Configure Libraries dialog box, the datetimeTag node appears in the tree on the left. Because no class library file has been specified for it, it is displayed in conspicuous red unlike other nodes. Click datetimeTag and switch the Library Settings setting page to Framework In the tab page, as shown below:
Figure 18 Switch to Framework |
Select the User-Defined JSP Tag Library option in the Framework drop-down box, click the Add... button at the bottom right of the tab page, and the Define New Tag Library dialog box will pop up, as shown in the figure below:
Figure 19 Description file specifying the tag library |
In the Define New Tag Library dialog box, click the... button after TLD file and navigate to the <project directory>/datetimeTag/taglibs-datetime.tld file. After confirmation, JBuilder automatically fills in the remaining settings. Generally, there is no need to change these JBuilder Automatic replenishment settings. Among them, Prefix specifies a reference prefix for this tag library. Click the OK button to return to the Configure Libraries dialog box. The datetimeTag node is displayed in normal color as shown below:
Figure 20 The effect after correctly configuring the tag library |
Click the OK button in the Configure Libraries dialog box to complete the configuration of the datetime tag library.
3. Reference this newly configured tag library in the current project.
Project->Project Properties...->Paths->Switch to the Required Libraries tab->Click the Add... button to select the datetimeTag from the JBuilder class library. After successful configuration, the Project Properties dialog box looks as follows:
Figure 21 Project reference library |
Create welcome JSP page
1. File->New…->Web->Double-click the JSP icon to start the Create JSP Wizard, specify the JSP file name as welcome, and click Next to proceed to the next step.
2. Reference the datetimeTag tag library in the welcome.jsp page.
In step 2 of the wizard, you are allowed to select various tag libraries in JBuilder. The datetimeTag tag library we configured in the previous section also appears in the Tag Libraries list, as shown in the following figure:
Figure 22 Reference tag library |
Expand the datetime Tag and check taglibs-datetime, and click Next to go to the next step.
3. Reference the userBean object placed in the session domain in switch.jsp.
Figure 23 References the userBean placed in session in switch.jsp |
Click Add Bean... to select the bookstore.User class, specify the Bean name as ses_userBean in the ID column, and select the session scope in the Scope column. ses_userBean is the name specified for userBean in switch.jsp. The Web container will search for the object in the session based on this name. If it cannot find it, it will create the bookstore.User object because welcome.jsp is called after switch.jsp. So unless the session is expired, the userBean object can be found.
Click Finish directly to create the welcome.jsp file. The code is as follows:
Code Listing 15 welcome.jsp welcome page
1. <%@ page contentType="text/html; charset=GBK" %> 2. <%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="dt" %> 3. <html> 4. <head> 5. <title> 6.welcome 7. </title> 8. </head> 9. <jsp:useBean id="ses_userBean" scope="session" class="bookstore.User" /> 10. <jsp:setProperty name="ses_userBean" property="*" /> 11. <body bgcolor="#ffffff"> 12. <h1> 13. JBuilder Generated JSP 14. </h1> 15. </body> 16. </html> |
The tag library referenced in step 2 of the wizard is set corresponding to the referenced tag library declaration code in line 2. The Bean set in step 3 corresponds to the 9th to 10th lines of code. Since there is no need to fill in the Bean value in welcome.jsp, the code on line 10 should be removed manually.
Next, we reference the datetime tag library in the welcome.jsp file and use it to generate a current time format string. Open the welcome.jsp file and switch to the Source view page. First clear the code generated by JBuilder in <body></body>, enter "<dt:" in <body></body>, JBuilder will use the TagInsight function to display all available tag items in this tag library, as follows As shown in the figure:
Figure 24 Using TagInsight to enter the tag library
TagInsight can be used to easily enter the available tags in the tag library, which greatly speeds up the code entry of the tag library and ensures correctness. In welcome.jsp, we use the tag library to obtain a current formatted time string. In addition, we also obtain the user's name through ses_userBean. The final code of welcome.jsp looks like this:
Code Listing 16 welcome.jsp references the tag library and Session object
1. <%@page contentType="text/html; charset=GBK" errorPage="error.jsp" %> 2. <%@taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="dt"%> 3. <html> 4. <head> 5. <title>welcome</title> 6. </head> 7. <jsp:useBean id="ses_userBean" scope="session" class="bookstore.User"/> 8. <body bgcolor="#ffffff">You are 9. <%=ses_userBean.getUserName()%>, welcome to log in. <br> 10. The current time is<dt:format pattern="MM/dd/yyyy hh:mm"><dt:currentTime/></dt:format> 11. <br>Click<a href="quit.jsp">here</a>to exit the system 12. </body> 13. </html> |
In addition, JBuilder copied the datetime tag library description file taglibs-datetime.tld to WEB-INF, and tampered with the web.xml file to declare the address of taglibs-datetime.tld:
Code Listing 17 Declaring the tag description file in web.xml
1. <?xml version="1.0" encoding="UTF-8"?> 2.… 3. <web-app> 4. <taglib> 5. <taglib-uri>http://jakarta.apache.org/taglibs/datetime-1.0</taglib-uri> 6. <taglib-location>/WEB-INF/taglibs-datetime.tld</taglib-location> 7. </taglib> 8. </web-app> |
As shown above, in lines 4 to 7 of web.xml, JBuilder automatically adds the description file of the datetime tag library so that the web container can correctly find the required information.
When compiling the project to generate a Web directory, JBuilder will copy the JAR file taglibs-datetime.jar of the datetime tag library to the WEB-INF/lib directory.
When the user logs in successfully, he will be redirected to the welcome.jsp page. The page effect is as shown below:
Figure 25 welcome.jsp effect page |
When the user clicks the link "here", it will be linked to quit.jsp. The quit.jsp page is responsible for clearing the session. After clearing the session, it will unbind the objects referenced in the session and release the resources.
quit.jsp exit processing page
Since the HTTP protocol works in a request/response manner, when the client exits the system, it needs to actively send a request to the Web server to notify the Web server to destroy the session in time. Otherwise, the Web server will only wait until the session expires before destroying it.
We use a quit.jsp to handle the user's exit from the system. quit.jsp is responsible for logging out the session and releasing resources in a timely manner.
·Log out the session.
·Close the browser window.
The code is as follows:
1. <%@ page contentType="text/html; charset=GBK" %> 2.<% 3. session.invalidate(); 4. %> 5. <script language="javaScript" > 6. window.opener = null; 7. window.close(); 8. </script> |
Line 3 is responsible for logging out of the session. The objects originally placed in the session will be unbound and wait for garbage collection to release resources. For this example, there is a userBean object named ses_userBean in the session (it is put into the session in switch.jsp). After calling session.invalidate(), the userBean is unbound from the session, and its valueUnbound( ) method will be triggered and then wait for garbage collection.
Lines 5 to 8 are a JavaScript script program responsible for closing the window. If the web page is not opened through a script program (window.open()), the window.opener object must be set before calling the window.close() script to close the window. Is null, as shown in line 6, otherwise the browser will pop up a dialog box confirming to close. The author found that this problem has troubled many Web programmers, so I pointed it out in particular.
Practical experience:
When the user exits the system, the session needs to be logged out, otherwise the session object will not be cleared until the session expires in the server. Assume that the maximum inactivity time of a session is 30 minutes (the default time). If the session objects are not cleared manually, the system resources occupied by these objects will not be released until 30 minutes have passed after a user exits the system. |