Using custom tags in a JSP can do that, in fact, the types of frameworks we know are basically implemented in the form of custom tags.
By using custom tags, we can display complex logic on the page with simple labels. Let's implement a very simple custom label that will lead you through this simple example into the domain of custom tags. The feature we're going to define for this custom tag is to display the copyright information in the JSP page, and we'll upgrade it in future posts.
First, open the editor and create the following Java code:
Copy Code code as follows:
Package Com.yanzhijun;
Import java.io.*;
Import javax.servlet.jsp.*;
Import javax.servlet.jsp.tagext.*;
public class Copyrighttag extends TagSupport
{
public int Doendtag ()
{
Try
{
String copypre = "Ye Zhijun All rights reserved ©2008";
String info = new String (Copypre.getbytes (), "iso8859_1");
Pagecontext.getout (). println (info);
}
catch (IOException e) {}
return eval_page;
}
}
After the above code edits are complete, the class file is compiled and generated. The compiled bytecode file is placed with the package name in the Web-inf\classes directory of the Web application that is ready to use the current label. For example, the WEB application is under the directory named Test, and the COM directory generated when the code is compiled is copied to test\web-inf\classes.
Note: When compiling the above code, if it is not done in an integrated environment, for example, by executing the JAVAC command directly at the command line, You need to manually add the location of the package javax.servlet.jsp to the environment variable classpath, and for Tomcat, the location of this package is Jsp-api.jar in the "Lib" directory under the Tomcat installation directory.
The following XML-formatted files are then deposited into a file named Testlib.tld, where the file testlib.tld is stored under the directory web-inf\tlds.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en"
"Http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd" >
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>yzj</shortname>
<tag>
<name>copyright</name>
<tagclass>com.yanzhijun.CopyRightTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute/>
</tag>
</taglib>
Now that we've completed a custom tag, we can then use it in the JSP file. For example, there are the following JSP files:
Copy Code code as follows:
<%@ taglib uri= "Web-inf/tlds/testlib.tld" prefix= "Yzj"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Custom Tags Simple example </title>
<body>
<p> here is the body content </p>
<yzj:copyright/>
</body>
Access to the JSP file in the browser, you can see, at the bottom of the page, the "Ye Zhijun copyright ©2008" information, which is our definition of this definition of the role of the label.
By using the above procedure, you have implemented a custom label and tested it. For questions about its principles, mechanisms, and details to be aware of, please wait for the following blog post.