Utility introduction to using custom tags in the JSP page
Level: primary
Kyle Gabhart (Kyle@gabhart.com) consultant, Gabhart Communications, December 2003
JSP custom labeled is a standardized mechanism that provides a standardization with business logic separation, so that the page designer can put the attention to the representation, while the application developer writes the rear code. in
In this article of J2EE Explorer, developer Kyle Gabhart with business concept introduces the basic knowledge of JSP custom tags and guides you to complete the five-step procedure that adds them to the JSP page.
You may have heard that there are hundreds of different ways now, but it is important to indicate logic and business logic when developing web applications. In recent years, the Java platform has been developed into this separation in the system structure level. For example, adding a JavaBean component in a JSP architecture to enable developers using JSP tags to obtain and set attributes on a particularly encoded Java component. These components or JavaBeans represent the representative layer to perform backend service processing.
The JSP custom tag is the product of the JSP / JavaBean architecture. Like JavaBean technology, custom tags help to separateize logic with business logic. And customizes the bridge between the HTML, XML, and JavaScript World and Software Engineers marked as a web designer, SQL calls, and algorithm world bridges.
In this month's J2EE explorer, you will learn some of the basic knowledge of JSP custom tags in the JSP page. Let's first introduce the JSP tag, then go directly to the creation tag, combine it into the JSP container and use it in the JSP page. In the end of this article, we will briefly discuss Java Standard Tag Library (JSTL), which is a set of standardized JSP custom tags that can be used in your Java development project.
JSP Custom Tags JSP Custom Tags are user-defined tags that follow a special XML syntax used by JSP JavaBean tags (ie Usebean, getProperty, and SetProperty). When the servlet container processes custom tags, one or more Java class files are called, which is basically the same as the JavaBean calls that use the Java class file to process the JSP page. After processing the tag, the container will take its name and attribute, and any content that may be in the body and pass it to one or more class files.
Java developers write tag handler classes to process tags and process all Java code and data operations required. For web designers, custom tags and standard HTML tags can be used to use backend dynamic data, they seem to have no difference in use. Correctly writing Custom Tags You can create, query, and operational data without writing a line Java code. Correct use of custom markers If Java developers do not have to consider the representation during the encoding process. Such a member of the application development team can focus on his or her best.
Implementing a JSP Custom Tag JSP Architecture requires the following components to implement custom tags:
One item in the JSP declaration web application descriptor (Web.xml) in each page is a JAR file that contains special XML files and Java classes called to process custom tags.
In the following sections, you will learn how to meet these requirements step by step and add custom tags to the JSP page. To successfully implement a JSP custom tag, you need to take the following five steps:
Write a tag handler class. Create a tag library descriptor (TLD). Enable TLD files and handler classes to access. Quote the tag library. Use tags in the JSP page.
These content are quite basic, nor will it take for a long time. Let us start. Step 1. Writing Mark Processing Program Class In the following example, we will use a very simple display example of the current time and date. Below is DateTag:
The first thing we have to do is to write a tag handler class. The JSP container determines each custom tag when performing the JSP page of the reference custom tag. When the container encounters a tag, it calls the tag handler associated with this custom tag, and we will discuss this process later later. Each tag handler then implements a special interface in the JSP API. There are two types of tags: tagged content (or body) can be handled and markups that cannot process tag content:
This Is Some Body Content That The Tag Handler CAN Operate Upon.
abc: tagwithbody>
You don't need to join the text in the DateTag example because it only shows the current date. Therefore, our handler class will implement a TAG interface (generally through the extended tagSupport class). If we want to create a tag that can handle the text, then we need to implement the BodyTag interface (generally through the extended bodytagsupport class). Listing 1 shows the processing program class of DateTag:
Listing 1. Tag handler class
Package mytags;
Import javax.servlet.jsp.tagext. *;
Import javax.servlet.jsp. *;
Import javax.servlet.http. *;
Import java.text. *;
Import java.util. *;
Public DateTag Extends tagsupport {
Public int desartTAG () throws javax.servlet.jsp.jspexception {
HTTPSERVLETREQUEST REQ;
Locale Locale;
HTTPJSPPAGE G;
DateFormat DF;
String Date;
Jspwriter Out;
REQ = (httpservletRequest) pageContext.getRequest ();
Locale = Req.getlocale ();
DF = SimpleDateFormat.getdateInstance
SimpleDateFormat.full, Locale);
Date = df.format (new java.util.date ());
Try {
OUT = PageContext.getut ();
Out.print (Date);
} catch (ioexception ie) {
Throw new JSPEXCEPTION ("I / O Error:" IOE.GetMessage ());
} // end try / catch
Return tag.skip_body;
} // end dostarttag ()
} // end datetag
The description of this code is worth mentioning about the DateTAG handler class. First look at the method statement. If we directly implement the TAG interface, then you need to complete several ways to declare. Because the Tagsupport class is simple, concrete class, it fully implements methods declared in the TAG interface, we can only implement those methods to use in custom tags. In this simple example, we only realize the dostartTag () method, which is called when you encounter the start tag. You may also notice that the dostarttag () method returns Skip_body. Of course, the reason is that our simple date tag has no body. The last important thing you have to pay attention to is to use the PageContext object access to the output cache to send content directly to the output stream. You may still remember that in this series, the PageContext object is an implicit object that provides access to the properties related to the current page.
After writing the source code file, we compile this class like compilation all other Java classes (make sure to add a servlet / jsp jar file in the classpath), then place the compiled class file into the web application class directory (web-infer in / classes. If we develop a few tags or define a tag tag, then we will have multiple tag handler classes. In this case we may choose to package the handler class in a JAR file instead of making them a dispersed file in the class directory.
Step 2. Creating a TLD Next is to define a library that contains the mapping between the Custom Tag and the Java class (or multiple classes) that handles it. This library is defined in an XML document called a marking library descriptor (TLD). We will call this TLD from the DateTag example datetaglib.tld. Note ".TLD" is the standard extension of this file.
Listing 2. DateTaglib.TLD file
Xml Version = "1.0" encoding = "ISO-8859-1"?>
tag>
taglib>
DateTaglib.TLD is an excellent, minimal tag library descriptor file. All key information is included in the TAG tag, which maps the tagname and handler class here, and we declare the sensitivity of the tag for the content. For more complex situations, we can use other XML tags to provide more information about libraries and tags. It is also common to define multiple tags in a library.
Step 3. Make TLD and Processor classes to access step 3 to make this class or these classes and TLDs can be accessed by web applications. There are two ways: You can pack classes and TLDs into a JAR file, and then store this JAR file in the lib directory of the web application, you can also place the class file into the classes subdirectory and put the TLD file. A location below the web-inflicity of the web application.
In this example, we will use the second method to deck the TLD file and class into the web application directory structure. You can recall in step 1 we have placed the tag handler class in the classes directory, so we actually still store TLD files. The TLD file is stored in a web-INF directory or subdirectory. If you are deploying a Java file, store the Meta-INF / directory or child directory in Jar. Here, we didn't use the JAR file, so we only store TLD to the web-inf / lib directory of the web application. Step 4. In this case, we have compiled the tag handler class, create a TLD file to define mappings between handler classes and tags, and ensure that classes and tags are accessible in applications. The next step is to establish a reference between the JSP page and the tag library. There are two ways to declare references between JSP pages and their libraries. You can declare a static reference through the web application descriptor (Web.xml), or you can declare a dynamic reference directly in the page. We will try these two methods.
To make a static reference, you must first add the following items to the web.xml file:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
taglib>
Web-app>
Then, add the JSP declaration to all the page you need to use the custom marker:
<% @ Taglib Uri = "MyTags" prefix = "ABC"%>
Note The specified URI property matches the TagLib-URI value specified in the web.xml file.
For dynamic reference, just add a JSP statement in all pages that need to use this library:
<% @ Taglib Uri = "/ Web-inf / lib / datetaglib.tld" prefix = "ABC"%>
When the comparison of static references and dynamic references is in the static reference of the tag library, the JSP statement must query the web.xml file to execute the library query. This means that if you move or rename the library, or you want to add more libraries in a web.xml file, you must stop the server, update the web.xml file, and then restart the server. The dynamic method allows the JSP page directly to the TLD position, thus processing when explaining the JSP page.
The static method provides a certain degree of non-directness between the actual names and positions of the page and the library, which can provide you with some flexibility to change these properties without modifying the page. On the other hand, the dynamic approach provides greater flexibility, allowing you to increase and move tag declaration at runtime. If you are interested in dynamic methods, but worry, you may have to update the maintenance burden of multiple pages, then you can always put the JSP declaration in a separate JSP file and to access every one. Add this page in the page of the WEB application's custom library. This allows you to increase the flexibility of the library only when you need to update the information at runtime.
Step 5. After using the tag in the JSP page, we can use these custom tags in the JSP page. Listing 3 shows a browser output containing a JSP page containing DateTag: Listing 3. JSP page with custom tags
<% @ Taglib Uri = "/ Web-inf / lib / datetaglib.tld" prefix = "ABC"%>
HEAD>
Hi Today Is
Body>
Html>
Restart the web server and test your JSP page! The results look similar to Figure 1.
Figure 1. DateTag's browser output
Save time with JSTL, you may have a custom tag feature that you need. The eighty-percent of 80 has been created and standardized by J2EE groups. Use an existing tag library instead of creating everything from beginning to save a lot of time and effort. Although there are dozens of libraries in the public domain, the industry brings together a specific custom library. The Java Standard Tag Library (JSTL) is designed by Java Community Process, and its reference implementation is developed and maintained by Apache Group through the Jakarta Taglibs project (see Reference).
JSTL defines tags for processing demand for common web applications, such as variable support, process control, URL management, XML operation, internationalization, database access, and more. In addition to a set of rich markers, JSTL also defines its own expression language (EL). EL allows us to easily access application data and more easily manipulate this data without using scripts or requests.
In addition to saving you to develop all markers from head, JSTL also has all the benefits of standardization and industry recognition. These benefits include manufacturers support, a large number of texts, and a great opportunity to find employees or contractors with JSTL experience.
Conclusion Inj2ee Web development increasingly needs to separate business and representation logic, and JSP custom tags provide an attractive approach to alternative simple old JavaBean and Java scripts. Better is a custom tag library that has already defined standards in JSTL. In this article, in J2EE Explorer, you have experienced a custom tag from head and implementing it in the JSP page. I also briefly introduced JSTL and elaborate the benefits of using it instead of creating all custom tags.
Next month, we will continue to explore J2EE technology to analyze the web application security architecture. I wish you a happy time!