In the Java EE Project, JSP pages often insert dynamic content by embedding scriptlets in a static page template. However, as complex programs grow, JSP pages become unmanageable. While developing small projects in this way is readily available, scriptlets still faces the following disadvantages:
Scriptlet difficult to read and modify. JSP pages with Scriptlets mix two languages, which makes reading and maintenance difficult.
Scriptlets encourages a mix of data performance and logical processing. The main function of JSP page is data performance rather than logic processing. Logic handles the work of Java classes, which should be maintained and reused by programmers.
Scriptlets can not be reused. When Scriptlets is reused, it is often encouraged to copy-paste to achieve reuse, which is a dangerous maintenance method. Every time you copy-paste scriptlets, there will be more lines of extra code to be maintained.
Scriptlets parameters are difficult to determine the delivery. In any case, a large number of people simply copy, paste, edit or similar increase, so that most of the extra code needs more maintenance.
Rather than creating a huge JSP page filled with scriptlets, consider using user-defined labels. User-defined tags allow you to create and use your own defined class HTML tags in your JSP. Whenever the JSP engine encounters a user-defined label, it automatically finds the label processing class and automatically invokes it. Custom labels on the page will be replaced by the output of the ticket processing class. This allows the JSP page to not write Java code directly on the page, you can specify the generation of dynamic content.
User-defined tags provide the n benefits of your web design: He improves the readability of the page's code. Page designers, rather than programmers, can use labels much easier than using scriptlets. The programmer who maintains the code needs only the personality tag library face is not a JSP page, so he does not risk damaging the appearance of the page. At each point in the use of the label, the enhanced or fixed label changes the performance of the label. The label is easier to determine than the scriptlets because it is being used as a property or being communicated in the label body. Finally, the label is more reusable than the scriptlets because you can create a shared, reusable, custom tag library. JSTL provides this standard set of custom tags.
Let's look at a simple JSP page example to see how to use a custom tag. The following page uses Scriptlet to get the data:
<%@ page import= "Java.text.SimpleDateFormat"%>
<%@ page import= "Java.util.Date"%>
<HTML>
<BODY>
<H3>
The date and time at the server are:
<%
String Sformat = "eeee, d MMMM yyyy ' at ' Kk:mm:ss z";
SimpleDateFormat format = new SimpleDateFormat (Sformat);
Date date = new Date ();
String sdate = Format.format (date);
Out.print (sdate);
%>
</H3>
</BODY>
</HTML>
This page is very simple and straightforward, although this simple function seems to have a lot of stuff. If you want to display a date on every page, you can only copy and paste the code to every page in the project. If you do this, then you need to maintain more than just the original copy of the code, but every copy you paste. If this piece of code appears on multiple pages, changing the time format will take up a lot of your time.
The following section is a very clean JSP page. Here, the Java code is moved out of the scriptlet and placed in a custom label:
<%@ taglib uri= "/web-inf/taglib.tld" prefix= "Mytags"%>
<HTML>
<BODY>
<H3>
The date and time at the server are: <mytags:date/>
</H3>
</BODY>
</HTML>
In this example, <@% taglib%> is used to indicate the custom tag descriptor file path (descriptor or TLD file in the tag library) and to define a namespace ("Mytags", but any character you like) for the tag name. The JSP engine recognizes <mytags:date/> as a user-defined label symbol, he will call the label processor for this tag and replace the label and content with the processing result.
Create a user-defined label processor
Creating a user-defined label processor requires more than a certain amount of work for a scriptlet because of this Java class, and you have to write a descriptor file for him in the TLD format (described in the next section). The following class Datatag implements a label processor:
public class DateTag extends TagSupport {
protected PageContext _pagecontext;
protected String _sformat;
Static final String _sformatdefault =
"Eeee, D MMMM yyyy ' at ' Kk:mm:ss z";
public void Setpagecontext (PageContext pagecontext) {
_pagecontext = PageContext;
_sformat = _sformatdefault;
}
Handle the tag
public int doStartTag () throws Jspexception {
SimpleDateFormat format =
New SimpleDateFormat (GetFormat ());
JspWriter out = _pagecontext.getout ();
Date date = new Date ();
String sdate = Format.format (date);
try {
Out.print (sdate);
catch (IOException ex) {
throw new Jspexception ("Datetag:can ' t write:" +
Ex.getmessage ());
}
return skip_body;
}
Handlers for "format" attribute
public void SetFormat (String sformat) {
_sformat = Sformat;
}
Public String GetFormat () {
return _sformat;
}
}
TagSupport implements all the interfaces required by the label processor. These methods have not done anything, label processor developers have refactored the methods needed to allow the base class to handle all other methods called. Each time the label processor is invoked, the Setpagecontext method is tuned. This class is used to simply save the PageContext reference later.
The doStartTag method is invoked when the JSP engine encounters this label. This method and the 1th version of the JSP page Scriptlet handled the same thing. He writes the results back to JspWriter, and the results include pagecontext that are stored first. All content addressed to JspWriter will be embedded directly into the response page. Note that doStartTag can only throw jspexception exceptions. If a write failure occurs, the original IOException is converted into a jspexception and thrown back. This method returns Skip_body, who tells the JSP container to discard the label content.
The last two methods of the label processor are SetFormat and GetFormat. The astute reader should have known his role. He was used by the Web container to set the label attribute value (discussed in more detail later). Here they are used to set date format attributes and output date format attributes.
Label definition: TLD file
A tag library descriptor file, or a TLD file, is an XML file. He used to describe the label in the tag library. The following is a description file for the DateTag tag.
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE taglib
Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en"
"Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>first</short-name>
<uri>http://www.elucify.com/j2eetips/sept2002/taglib</uri>
<description>sample Tag library</description>
<tag>
<name>date</name>
<tag-class>com.elucify.tips.sep2002.DateTag</tag-class>
<body-content>empty</body-content>
<description>print the date with a compiled-in format</description>
<attribute>
<name>format</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
These descriptors provide information about this tag library. He also provides information about each label, such as the label name, the processor's class, and the label description. This file is used to replace the war file in the Web-inf directory and to provide references for JSP pages using these tags.
Add Properties
Notice that TLD file shown above defines a attribute called format. This is the string passed to SimpleDateFormat the date is printed. The If A format attribute is present on a date tag, the JSP engine calls SetFormat on the handler class. Otherwise, the handler class uses a default format. Attributes provide a great deal of customizability to custom tags. For example, the following JSP page uses the "format" to format the "date in several different ways on the same Pag E:
Note The property invocation format shown above for this TLD file. This is a string that is passed to SimpleDateFormat to control how the date is displayed. If a date format is provided to the date tag, the JSP engine invokes the SetFormat method in the Controller class, otherwise the label processor uses the default format. property provides a large number of customizable properties for user-defined labels. For example, the following example, on the same page, uses several different formatting attributes to format the time.