Simple example of one of the JSP custom tags

  • 2020-05-07 20:14:21
  • OfStack

This can be done with custom tags in JSP, and in fact, most of the frameworks we're familiar with do so in the form of custom tags.
By using custom tags, we can implement complex logic in the page with simple tags to show. Next we will implement a very simple custom tag, with the hope that this simple example will lead you into the field of custom tags. The custom tag we are going to define will display the publishing rights information on the JSP page, and we will update it in future posts.
First, open the editor and create the following Java code:
 
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 = " All rights reserved  ©2008"; 
String info = new String(copyPre.getBytes(), "ISO8859_1"); 
pageContext.getOut().println(info); 
} 
catch(IOException e){} 
return EVAL_PAGE; 
} 
} 

When the above code editing is complete, the class file is compiled and generated. And put the compiled bytecode file together with the package name 1 under the WEB-INF \classes directory of the WEB application that is ready to use the current label. For example, if WEB is applied under a directory named test, then copy all the com directory generated when the above code is compiled to test\WEB-INF\classes.
Note: when compiling the above code, if it is not done in an integrated environment, such as directly from the command line by executing the javac command, you need to manually add the location of the package javax.servlet.jsp to the environment variable CLASSPATH, which, for Tomcat, is jsp-api.jar in the lib directory under the Tomcat installation directory jsp.
Then save the following XML files into a file named testlib.tld. The file testlib.tld is stored under the directory WEB-INF \tlds.
 
<?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> 

At this point, we have a custom tag and are ready to use it in the JSP file. For example, there is the following JSP file:
 
<%@ taglib uri="WEB-INF/tlds/testlib.tld" prefix="yzj" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title> Simple example of a custom tag </title> 
</head> 
<body> 
<p> Here is the body content </p> 
<yzj:copyright/> 
</body> 
</html> 

In the browser to access the JSP file, you can see, at the bottom of the page, displayed "yan zhijun all rights reserved & copy; "2008" information, which is exactly what we define this self-defining tag to do.
Through the above procedure, custom tags have been implemented and tested for their use. Please wait for the next post on the principle, mechanism, details to note, etc.

Related articles: