Although the use of Scriplet and JavaBean in JSP can complete a lot of dynamic operations such as: Accessing the database, implement E-mail, bbs, and more. It is very difficult to use Scriplet to insert Java code in JSP, but this also has a certain difficulty of maintaining future maintenance. As an alternative JSP provides a mechanism called a custom label, using this mechanism you can extend JSP in your needs, with your own defined tag package, make JSP pages more simple, More easily develop and maintain.
Once you have customized your tag, you can use them to develop it like SUN. Its syntax is the same as
Since there are so many benefits, let's take a look at what is Custom Tag. The definition of Sun is the JSP tag defined by the user (is a user-defined Jsp Language ELEMENT). And there are some features: 1. The parameters are obtained from the request page. 2. You can access objects that all JSP pages can be accessed. 3. You can modify the Response generated by the request page. 4. Can interact with each other. You can use a javabean that makes a tag and use it in another TAG. 5. Mutual nested. (就
head> html> Meaning)Below we will learn from a simple example to learn from JSP TAG.
To use tag, you must first complete the following things: 1. Declare a tag library. 2. Implement the declared tag library.
To declare the tag library first to create a TLD (Tag Library Descriptor) file, this is an XML file, but with TLD is extension, such as Database.tld. The establishment of a good TLD file is generally placed in Web-INF (mentioned in the previous two articles) directory.
Ok, now we build a TLD file in Web-Inf, such as Database.tld. Then the following code COPY to the beginning of the TLD file: XML Version = "1.0" Encoding = "ISO-8859-1">
Code 3
The above is that each TLD file is in place that is essential. As for web-jsptaglibrary_1_2.dtd This file you can find in the Tomcat4 or updated version in% Catalina_Home% / common / lib servlet.jar (if you are interested, see if you don't look), the specific location is Servlet.jar / Javax / Servlet / JSP / Resources, and you will find an old version under this folder: web-jsptaglibrary_1_1.dtd. But Sun's documentation is recommended to use new versions as much as possible. (All the TLD files that have occurred later, if they do not declare, they are assumed to add top 3).
*********************************************************** ************************* (I have to specifically explain the opening in the process of studying: XML Version = "1.0" Encoding = "ISO-8859-1"> If you use the beginning to compile, you will tell you the external file http file: //java.sun.com/j2ee/dtd/WEB-JSPTAGLIBRARY_1_2.DTD, I It may be that the above URI is used in the enterprise version, and I didn't install J2EE. If I said not to ask prawn to correct, my e-mail: flyhellenessuanwei@yahoo.com.cn. **** *********************************************************** ***********************
With the above foundation, we can add content to the document just established.
Code 4 taglib is the root of the TLD document (only one). TLIB-VERSION indicates the version of the tag, the same JSP-Version indicates the version of JSP. The URI gives a unique URI to locate the tag library. Description is a short description of the tag library. (There are also some optionally interested people to refer to Sun's document http://java.sun.com/webservices/docs/1.0/tutorial/doc/jsptags.html).
One or more TAG elements can be inserted in Code4. For example, we join: ...
Code 5
The names in the TAG element are specified for the TAG you define, and the tag-class is used to indicate the Class name that handles this tag. Body-content points out the type of content allowed between
XML Version = "1.0" encoding = "ISO-8859-1">
Let's implement the TEMP.SIMTAG class defined above.
Package temp;
Import javax.servlet.jsp.tagext. *; import javax.servlet.jsp. *;
Public class simtag extends tagsupport {private static int guestNum = 0;
Public int dostarttag () throws jspexception {tryContext.getut (). Println ("Hello!"); PageContext.getut (). Println ("You are the" guestnum "st guset!");} Catch (Exception EX) {throw new jsptagexception ("simpletag:" ex.getMessage ());} Return Skip_body;
Public int doendtag () {returnval_page;}}
Code 5
Maybe you don't understand the following, it doesn't matter, let's take a look at the results. Compile the code, and then create a folder called Classes in the web-INF folder mentioned in the first partial article, and then add the following code to the CLASS folder after compiling. Add the following code. The Web.xml established in the first article is as follows:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
Code 6
This is to make the server know how to find your DATABASE.TLD (remember to put it in Web-IRI) by the logical name defined by taglib-uri. You can use "/Database.tld" logical name to quote Database.TLD.
Ok, I will notice it immediately, and the Hello.jsp of your original written is as follows:
<% @ Taglib Uri = "/ Database.tld" prefix = "database"%> <% @ page contentty = "text / html; charSet = GBK"%> <% @ page import = "java.io. *"% >
Code 7
OK! Now your home directory should be the following status:
Home | | JSP | | Hello.jsp | | Web-INF | Classes | | Temp | | Simtag.class | Web.xml | Database.tld
Ok, run Your Tomcat (see the first article in 127.0.1:8080/HOME/jsp/hello.jsp You can see the results. You will find that the result is the same as the last JSP completed in the second article, just the refreshing page. In the next part of the article, we will learn more about Custom Tag, and the meaning of Code5 in Code5 is obvious. Goodbye after the holiday!
There is any opinions or refer to you, please contact me with my e-mail: flyhelleryuanwei@yahoo.com.cn.