- Create a class that introduces an external Jsp-api.jar package (available in Tomcat under the Lib package), which inherits the class Simpletagsupport
- Override the Dotag () method.
The Hellotag class under the JSPPROJEC package:
1 Public classHellotag extends simpletagsupport{2 3 @Override4 Public voidDotag () throws Jspexception, IOException {5 6 7JspWriter out= This. Getjspcontext (). Getout ();8 out. Print ("Hello, I'm a custom label .");9 }Ten One}
- Define the configuration file, which is placed in the tags package in the WebContent directory with the file name extension. tld.
<taglib xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version= "2.1" >
<tlib-version>1.2</tlib-version>
<short-name>my</short-name>
<uri>my/jsp/jstl/my_hi</uri>
<tag>
<name>hi</name> /Tag name
<tag-class>jspprojec.hellotag</tag-class> //Custom Label class
<body-content>Empty
</tag>
</taglib>
- To use a custom label in a JSP:
1<% @page import="Java.util.arraylist,java.util.arraylist,jspprojec. Student"%>2<%@ page language="Java"Contenttype="text/html; Charset=utf-8"3pageencoding="UTF-8"%>4<%@ taglib prefix= "My" uri= "Tags/my.tld"%>5<! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD">678<meta http-equiv="Content-type"Content="text/html; Charset=utf-8">9<title>insert title here</title>Ten One<body> A - <my:hi/> - the</body> -Results:
User-defined tags for JSP