0% found this document useful (0 votes)
17 views3 pages

Session 13

The document discusses custom tags in JSP. Custom tags allow separating business logic from the JSP page and can be reused. The document explains how to create a custom tag class, tag library descriptor (TLD) file, and use the tag in a JSP page by calling out to print the current date and time.

Uploaded by

shubhtiwari882j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Session 13

The document discusses custom tags in JSP. Custom tags allow separating business logic from the JSP page and can be reused. The document explains how to create a custom tag class, tag library descriptor (TLD) file, and use the tag in a JSP page by calling out to print the current date and time.

Uploaded by

shubhtiwari882j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Custom tags are user-defined tags.

They eliminates the possibility of scriptlet tag


and separates the business logic from the JSP page.
The same business logic can be used many times by the use of custom tag.
Syntax to use custom tag
There are two ways to use the custom tag. They are given below:
1. <prefix:tagname attr1=value1....attrn=valuen />

1. <prefix:tagname attr1=value1....attrn=valuen >


2. body code
3. </prefix:tagname>

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:

Field Name Description

public static int EVAL_BODY_INCLUDE it evaluates the body content.

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.

Example of JSP Custom Tag


In this example, we are going to create a custom tag that prints the current date
and time. We are performing action at the start of tag.
For creating any custom tag, we need to follow following steps:
1. Create the Tag handler class and perform action at the start or at the end of
the tag.
2. Create the Tag Library Descriptor (TLD) file and define tags
3. Create the JSP file that uses the Custom tag defined in the TLD file

The PageContext class provides getOut() method that returns the instance of JspWriter
class. TagSupport class provides instance of pageContext bydefault.

1) Create the Tag handler class


package MyPackage;

import javax.servlet.jsp.tagext.*;

import javax.servlet.jsp.*;

import java.io.*;

HelloTag—class file

public class HelloTag extends SimpleTagSupport {

public void doTag() throws IOException

JspWriter out=getJspContext().getOut();

out.print("Hello To Custom Tag World..");

2) Create the TLD file


Tag Library Descriptor (TLD) file contains information of tag and Tag
Hander classes. It must be contained inside the WEB-INF directory.
Right click on WEB-INF add new file mytags.tld
Code:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">

<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:

You might also like