Session 13
Session 13
JspTag interface
The JspTag is the root interface for all the interfaces and classes used in custom tag.
It is a marker interface.
Tag interface
The Tag interface is the sub interface of JspTag interface. It provides methods to
perform action at the start and end of the tag.
Fields of Tag interface
There are four fields defined in the Tag interface. They are:
public static int EVAL_PAGE it evaluates the JSP page content after the custom t
public static int SKIP_BODY it skips the body content of the tag.
public static int SKIP_PAGE it skips the JSP page content after the custom tag.
The PageContext class provides getOut() method that returns the instance of JspWriter
class. TagSupport class provides instance of pageContext bydefault.
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
HelloTag—class file
JspWriter out=getJspContext().getOut();
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>myt7ags</short-name>
<uri>mytags.tld</uri>
<tag>
<name>Hello</name>
<tag-class>MyPackage.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3) Create the JSP file
Let's use the tag in our jsp file. Here, we are specifying the path of tld file directly. But
it is recommended to use the uri name instead of full path of tld file.
It uses taglib directive to use the tags defined in the tld file.
Add jsp file(customdemo.jsp)
Code:
<%@ taglib uri="WEB-INF/mytags.tld" prefix="ex" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<ex:Hello/>
</body>
</html>
Output: