Example & Tutorial understanding programming in easy ways.

What is custom tags,how to use it and create it in jsp?.

 User-defined tags are known as custom tags. In this section we will see how to create a custom tag and use it in JSP.

To create a custom tag we need three things:
1) Tag handler class: In this class we specify what our custom tag will do when it is used in a JSP page.
2) TLD file: Tag descriptor file where we will specify our tag name, tag handler class and tag attributes.
3) JSP page: A JSP page where we will be using our custom tag.

Example:
In the below example we are creating a custom tag Myindia which will display the message “This is my india” when used in a JSP page.

Tag handler class:

A tag handler class should implement Tag/IterationTag/ BodyTag interface or it can also extend TagSupport/BodyTagSupport/SimpleTagSupport class. All the classes that support custom tags are present inside javax.servlet.jsp.tagext. In the below we are extending the class SimpleTagSupport.

example:

test.java

package jsptest.com;

import javax.servlet.jsp.tagext.*;

import javax.servlet.jsp.*;

import java.io.*;

public class Details extends Simplecustomtest {

public void doTag() throws JspException, IOException {

/*This is just to display a message, when

* we will use our custom tag. This message

* would be displayed

JspWriter out = getJspContext().getOut();

out.println("This is my india");}}

TLD File:
This file should present at the location: Project Name/WebContent/WEB-INF/ and it should have a .tld extension.

Note:
tag: custom tag name. In this example we have given it as Mynote
tag: Fully qualified class name. Our tag handler class note.java is in package jsptest.com so we have given the value as jsptest.note.

example:

note.tld

1.0

2.0

<short-name>My Custom testshort-name>

this is custom test

Myindia

class>class>

empty

Using custom tag in JSP:

Above we have created a custom tag named Myindia. Here we will be using it.
Note: taglib directive should have the TLD file path in uri field. Above we have created the note.tld file so we have given the path of that file.
Choose any prefix and specify it in taglib directive’s prefix field. Here we have specified it as myprefix.
Custom tag is called like this: . Our prefix is myprefix and tag name is Myindia so we have called it as in the below JSP page.

<%@ taglib prefix="myprefix" uri="WEB-INF/note.tld"%>

my custom test in jsp

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!