JBuilder2005 Practical JSP Special page

Source: Internet
Author: User
Tags define exit current time datetime garbage collection tld
js| page Welcome Page welcome.jsp

When the user enters the correct password, switch.jsp controls the JSP page to guide the welcome.jsp Welcome page, in this section, we develop this welcome.jsp page. Because welcome.jsp needs to use a Third-party tag library, you need to make some configuration in JBuilder to introduce this tag library before developing the welcome.jsp.

Configure a Third-party tag library into JBuilder

Apache Open source organization provides a lot of useful tag library, welcome page welcome.jsp need to use a DateTime tag library from Apache, you can use this tag library in the JSP to provide a variety of time display, download the address is: http:// Apache.justdn.org/jakarta/taglibs/datetime/binaries/jakarta-taglibs-datetime-1.0.1.zip.

The tag library typically consists of two files, one is the class package jar file, and the other is a label description file with a. tld extension. After extracting from the downloaded compressed document, we put the Taglibs-datetime.jar and Taglibs-datetime.tld files under the Engineering directory >/datetimetag directory.

To use a Third-party tag library in your project, you must configure the tag library in JBuilder and refer to it in the project. The configuration tag library is similar to the Configuration class library and is done through the Configure Libraries dialog box. The JBuilder class library and tag library are listed in the left tree in the Configure Libraries dialog box, and the class library is displayed as an icon, and the tag library is displayed as an icon. Here we will configure the DateTime tag library into JBuilder.

1. Tools->configure->libraries->configure Libraries dialog box.

Click on the add in the lower left corner of the Configure Libraries dialog box. button to eject the New Library Wizard dialog box, as shown in the following illustration:


Figure 17 New Library Wizard dialog box
In name, give the library a name: Datetimetag, press OK to return to the Configure Libraries dialog box.

2. Specifies 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, as opposed to the other nodes showing a red color, click on the Datetimetag and the library The settings page switches to the Framework label page as shown in the following illustration:


Figure 18 Switching to the framework
Select the user-defined JSP Tag Library option in the Framework dropdown box and click on the add at the bottom right of the tab page ... button, pop-up define the New Tag Library dialog box, as shown in the following figure:


Figure 19 Specifying a description file for the tag library
In the Define New Tag Library dialog box, click the TLD file ... button, navigate to the project directory >/datetimetag/taglibs-datetime.tld file, confirm JBuilder automatically populate the remaining settings items, generally do not need to change the JBuilder these automatic supplemental settings. Where prefix specifies a reference prefix for this tag library. Click the OK button to return to the Configure Libraries dialog box, the Datetimetag node appears as normal color as shown in the following image:


Figure 20 Effect of correctly configuring a tag library
Click the OK button in the Configure Libraries dialog box to complete the configuration of the datetime tag library.

3. Refer to the tag library for this new configuration in the current project.

Project->project properties...->paths-> switch to Required Libraries tab page-> Click Add ... button to select Datetimetag from the JBuilder class library. After the configuration is successful, the Project Properties dialog box looks like this:


Fig. 21 Project Reference Library
Create a welcome JSP page

1. file->new...->web-> Double-click the JSP icon, start the Create JSP Wizard, specify the JSP file name welcome, click Next to Next.

2. Reference the Datetimetag tag library in the welcome.jsp page.

In the 2nd step of the wizard, you are allowed to select various tag libraries in JBuilder, and the Datetimetag tag library that we configured in the previous section also appears in the Tag libraries list, as shown in the following illustration:


Figure 22 referencing the tag library
Expand datetime Tag and tick taglibs-datetime, press next to step.

3. Refers to the Userbean object that is placed in the session field in switch.jsp.


Figure 23 refers to the Userbean of the session in switch.jsp
Click Add Bean ... Select Bookstore. User class, specify the name of the bean in the ID bar to select the session scope in the Ses_userbean,scope column. Ses_ Userbean is the name specified in switch.jsp for Userbean, the Web container will look for objects in the session according to that name, and if they cannot be found, the Bookstore.user object is created because welcome.jsp is invoked after switch.jsp, so The Userbean object can be found unless the session is on.

Click Finish to create the welcome.jsp file, whose code looks like this:

Code List 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. The 4. The 5. The <title>
6. Welcome
7. The </title>
8. The 9. <jsp:usebean id= "Ses_userbean" scope= "session" class= "bookstore". User "/>
<jsp:setproperty name= "Ses_userbean" property= "*"/>
<body bgcolor= "#ffffff"
A. JBuilder generated JSP
. </body>
. The tag library referenced in step 2nd of the wizard sets the reference Tag library declaration code for line 2nd. The beans set in step 3rd correspond to the 9th to 10th line of code, because you do not need to populate the bean's value in welcome.jsp, so you should manually remove the line 10th code.

Below we reference the DateTime tag library in the welcome.jsp file to generate a current time format string. Open the welcone.jsp file and switch to the Source view page. First clear the code generated by JBuilder in <body> </body> and enter "<DT:" in <body> </body>, JBuilder will use the Taginsight feature to display all the available label items in this tag library, as shown in the following illustration:

    
Fig. 24 using Taginsight Input Tag Library

The use of Taginsight can be easily entered in the tag library of the available tags, greatly accelerate the tag library code entry and ensure correctness. In welcome.jsp we use the tag library to get a current formatting time string, and we also get the user's name through Ses_userbean. The final code for the welcome.jsp is as follows:

Code list welcome.jsp referencing tag libraries and session objects

1. <% @page contenttype= "text/html; CHARSET=GBK "errorpage=" error.jsp "%>
2. <% @taglib uri= "http://jakarta.apache.org/taglibs/datetime-1.0" prefix= "DT"%>
3. The 4. The 5. <title> Welcome </title>
6. The 7. <jsp:usebean id= "Ses_userbean" scope= "session" class= "bookstore". User "/>
8. <body bgcolor= "#ffffff" > > you are
9. <%=ses_userbean.getusername ()%>, Welcome to login. <br>
10. Now the time is <dt: Form at pattern= "mm/dd/yyyy hh:mm" > <dt:currentTime/> </dt: Form at>
One. <br> Click <a Href= "Quit.jsp" > here </a> exit the system
A. </body>
In addition, JBuilder the datetime tag Library description file Taglibs-datetime.tld to Web-inf, and tampered with Web.xml file, declaring Taglibs-datetime.tld address:

Declaration tag Description file in code listing Web.xml

1. <?xml version= "1.0" encoding= "UTF-8"?
2...
3. The <web-app>
4. The <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. The </taglib>
8. The </web-app>
As shown above, line 4th to 7th of Web.xml, JBuilder automatically joins the description file of the datetime tag library so that the Web container correctly finds the information it needs.

When the build Web directory is compiled by the project, JBuilder copies the jar file Taglibs-datetime.jar of the datetime tag library to the Web-inf/lib directory.

When the user logs on successfully, it will be turned to the welcome.jsp page, whose page effect is shown in the following image:


Image welcome.jsp Effect page
When the user clicks "Here" the link, will link to the quit.jsp,quit.jsp page is responsible for clears the session, after clears the session, will refer to the session the object to bind and releases the resources.
 
quit.jsp Exit Processing page

Because the HTTP protocol works in a request/response manner, the client exits the system with the initiative to send a request to the Web server notifying the Web server to destroy the session in a timely manner, otherwise the Web server will only destroy it when it expires.

We use a quit.jsp to handle the user to exit the system operation, QUIT.JSP is responsible for cancellation session, timely release of resources.

• Logout session.

• Close the browser window.

The code looks like this:

1. <%@ page contenttype= "text/html; CHARSET=GBK "%>
2. <%
3. Session.invalidate ();
4.%>
5. <script language= "JavaScript"
6. Window.opener = null;
7. Window.close ();
8. The </script>
The 3rd row is responsible for unregistering the session, and the object that was originally placed in session will be unbound, waiting for garbage collection to free up resources. For this example, there is a Userbean object named Ses_userbean in the session (it is put into session in switch.jsp), and after calling Session.invalidate (), Userbean is unbound from session, its Valueunbound () method is invoked and then waits for garbage collection.

Line 5th to 8th is a JavaScript script that closes the window, and if the page is not opened through a script (window.open ()), call the Window.close () script to close the window before You must first set the Window.opener object to null, as shown in line 6th, otherwise the browser pops up a dialog box that is OK to close, and the author finds that this problem has plagued many web programmers, so it is particularly pointed out.

Practical experience:

When the user exits the system, the session needs to be logged off, otherwise the sessions object will not be purged until after the session expires in the server. Suppose a session is inactive for a maximum of 30 minutes (the default), and if the session object is not purged manually, the system resources occupied by those objects will be freed after a user exits the system after 30 minutes.

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.