- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
I want to create a custom tag in JSP. How to write a custom tag in JSP?
A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page's servlet is executed.
JSP tag extensions let you create new tags that you can insert directly into a JavaServer Page. The JSP 2.0 specification introduced the Simple Tag Handlers for writing these custom tags.
To write a custom tag, you can simply extend the SimpleTagSupport class and override the doTag() method, where you can place your code to generate content for the tag.
Create "Hello" Tag
Consider you want to define a custom tag named <ex: Hello> and you want to use it in the following fashion without a body −
<ex:Hello />
To create a custom JSP tag, you must first create a Java class that acts as a tag handler. Let us now create the HelloTag class as follows −
package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } }
The above code has simple coding where the doTag() method takes the current JspContext object using the getJspContext() method and uses it to send "Hello Custom Tag!" to the current JspWriter object
Let us compile the above class and copy it in a directory available in the environment variable CLASSPATH. Finally, create the following tag library file: <Tomcat-Installation-Directory>webapps\ROOT\WEB-INF\custom.tld.
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
Let us now use the above-defined custom tag Hello in our JSP program as follows −
<%@ taglib prefix = "ex" uri = "WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello/> </body> </html>
Call the above JSP and this should produce the following result −
Hello Custom Tag!
- Related Articles
- How can I create custom tag in JSP which can accept attribute from parent jsp page?
- What are the standard attributes that should be passed to a custom tag in a JSP page?
- How to apply if tag in JSP?
- How to apply choose tag in JSP?
- How to apply forEach tag in JSP?
- How to apply forTokens tag in JSP?
- What is the use of tag in JSP?
- What is the use of tag in JSP?
- What is the use of tag in JSP?
- I am facing problem in using include directive tag in jsp. Please share a working example.
- I want to use %> literal in JSP page. But it is throwing error. How to escape this syntax in JSP?
- How to write a JSP Expression?
- How to write a comment in a JSP page?
- How to create a Hit Counter in JSP?
- How to write a for loop in a JSP page?
