js|web| Program
Label processor
The traditional label processing API in JSP 1.2 is complicated by allowing the tag body to contain scriptlet, but now it is possible to write a JSP Web page that contains no scriptlet by using an expression language. Finally, JSP 2.0 introduces a new tag extension mechanism called "simple label Extension", which is used in two ways:
- Java developers can define classes that implement Interface Javax.servlet.jsp.tagext.SimpleTag;
- Web page writers who don't know Java can use tag files.
First, look at the first way, code example 6 gives a simple label processor, which is simply to print "This is I-tag! ”。
code example 6: Hellotag.java
Package Jsp2.examples.simpletag;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.tagext.simpletagsupport;import java.io.IOException;
/**
* Simpletag handler that prints ' This was my I-tag! '
*/
public class Hellotag extends Simpletagsupport {
public void Dotag () throws Jspexception, IOException {
Getjspcontext (). Getout (). Write ("This is me-a-tag!");
}
}
Once the compilation is successful, the next step is to define a tag descriptor in the TLD, followed by an example of a label descriptor.
code example 7: Label descriptor
<tag>
<description>prints This is my tag</description>
<name>hello</name>
<tag-class>jsp2.examples.simpletag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
Finally, write a JSP page file using the label above, see code example 8.
code Example 8: helloworld.jsp
<%@ taglib prefix= "MyTag" uri= "/web-inf/jsp2/jsp2-example-taglib.tld"%>
<HTML>
<HEAD>
<title>simple Tag handler</title>
</HEAD>
<BODY>
<P>
<b>my-Prints</b>: <mytag:hello/>
</BODY>
</HTML>
To run this example:
- Copy the Hellotg.java and save it to the C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\classes\jsp2\examples\simpletag;
- Compile Hellotag.java using Javac;
- Add the label descriptor in code example 7 to the </taglib> before the file C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\jsp2\jsp2-example-taglib.tld;
- Copy the helloworld.jsp and save it to the c:\Tomcat5.0\webapps\jsp-examples\jsp2-tutorial directory;
- Open the helloworld.jsp in the browser.
If everything works, you should see something like the picture shown in Figure 4.
Figure 4 : Simple Label processor
Label file
Another way to use the simple tag extension mechanism is through the tag file. A tag file is a resource file that a Web page author can use to extract a section of JSP code that enables the reuse of code through custom functions. In other words, the tag file allows JSP page authors to create reusable tag libraries using JSP syntax. The label file extension must be ". Tag".
To illustrate how easy it is to use a label file, consider code example 9. Yes, this is a tag file!
code Example 9: Greetings.tag
Hello there. How are you doing?
Once you have defined the label file, you can use this custom feature in the authoring of JSP pages. For example, the JSP page in code example 10.
code sample: chat.jsp
<%@ taglib prefix= "tags" tagdir= "/web-inf/tags"%>
<HTML>
<HEAD>
<TITLE>JSP 2.0 Examples-hello World Using a Tag file</title>
</HEAD>
<BODY>
<P>
<b>the output of my-tag file Is</b>: <tags:greetings/>
</BODY>
</HTML>
To run this example:
- Copy the label file Greetings.tags and save it in the C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\tags directory;
- Copy JSP page file char.jsp and save it in c:\Tomcat5.0\webapps\jsp-examples\jsp2-tutorial directory;
- Open the chat.jsp file in a browser.
If everything works, you should see a window similar to that shown in Figure 5.
Figure 5 : Simple label file
Note : It is important to note that there is no TLD written for the greeting tag, but instead creates a tag file and places it in a special directory, and then imports it using the taglib instruction and uses it directly.
Another example of a tag file
The label file can be used as a template. Consider the tag file Display.tag in code example 11, which is rewritten based on the example of the panel in Tomcat 5.0. The instruction attribute is similar to the <attribute> element in TLD, allowing the declaration of custom Action attributes.
code example: Display.tag
<%@ attribute name= "COLOR"%>
<%@ attribute name= "bgcolor"%>
<%@ attribute name= "title"%>
<table border= "0" bgcolor= "${color}" > <TR>
<TD><B>${title}</B></TD>
</TR>
<TR>
<TD bgcolor= "${bgcolor}" >
<jsp:doBody/>
</TD>
</TR>
</TABLE>
Code Example 12 gives a simple JSP page that uses the label above.
code sample: newsportal.jsp
<%@ taglib prefix= "tags" tagdir= "/web-inf/tags"%>
<HTML>
<HEAD>
<title>another Tag File example</title>
</HEAD>
<BODY>
<table border= "0" > <tr valign= "Top" >
<TD>
<tags:display color= "#ff0000" bgcolor= "#ffc0c0"
title= "Travel" >
Last French Concorde arrives in ny<br/>
Another Travel headline<br/>
Yet Another Travel headline<br/>
</tags:display>
</TD>
<TD>
<tags:display color= "#00fc00" bgcolor= "#c0ffc0"
title= "Technology" >
Java for in-flight entertainment<br>
Another Technology headline<br>
Another Technology headline<br>
</tags:display>
</TD>
<TD>
<tags:display color= "#ffcc11" bgcolor= "#ffffcc"
title= "Sports" >
American football<br/>
Nba<br/>
Soccer<br/>
</tags:display>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
To run this example:
- Copy the file Display.tag and save it under C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\tag;
- Copy the file newsportal.jsp and save it to the c:\Tomcat5.0\webapps\jsp-examples\jsp2-tutorial;
- Open the Newsportal file in a browser.
The result should be a picture similar to Figure 6.
Figure 6 : Use the label file as a template
Conclusion
JSP 2.0 makes it easier to quickly develop and maintain dynamic Web pages, although the word "Java" appears in JSPs, but with JSP2.0, web authors can develop new dynamic Web pages without learning the Java programming language itself. The examples in this article illustrate how easy it is to develop dynamic Web pages using the new features of JSP2.0.