Breaking News
Loading...

how to use and create JSP (Java Server Pages) Custom tag

Share on Google Plus

JSP (Java Server Pages) Custom tag

JSP (Java server pages) custom tags separate the program logic and presentation of tag. Application designer uses the JSP custom tags in JSP page. Application developer develops the logic of custom tag. Lets begin on how to create and use JSP custom tags.

Use of JSP custom tag

1.     JSP custom tag use in JSP page

2.     It reduces coding in JSP pages.

3.     Application designer easily work on JSP pages

4.     It is reusable

Steps for creating JSP tag

1.     Create the tag handler class

2.     Create the TLD (Tag library descriptor)file

3.     Create the JSP file.

 

Packaging

Root directory (Current_Date_and_Time)

                Directory (WEB-INF)

                                Directory (classes)

                                                Tag handler class(MyTag.class)

                                Directory (src)

                                                Tag handler java file(MyTag.java)

                                TLD file(mydate.tld)

                JSP file(index.jsp)

 

Example

Print current date and time

 

Tag handler class

MyTag.java

 

package abc;

import java.io.*;

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.util.Calendar;

public class MyTag implements Tag

{

                PageContext pagecontext;

                Tag parent;

                public void setPageContext(PageContext pagecontext)

                {

                                this.pagecontext=pagecontext;

                }

                public void setParent(Tag p)

                {

                                parent=p;

                }

                public Tag getParent()

                {

                                return parent;

                }

                public int doStartTag() throws JspException

                {

                                try

                                { 

                                                JspWriter out=pagecontext.getOut();

                                                Calendar cal=Calendar.getInstance();

                                                out.print(cal.getTime());//print date and time 

                                }

                                catch(IOException e)

                                {

                                                System.out.println(e);

                                } 

                                return SKIP_BODY;

                }

                public int doEndTag() throws JspException

                {

                                return EVAL_PAGE;

                }

                public void release()

                {

                }

 

}

 

 Copy this code and paste into MyTag.java file.

 

TLD file

Mydate.tld

 

<taglib>

                <tlibversion>1.0</tlibversion>

                <jspversion>2.1</jspversion>

                <tag>

                                <name>date</name>

                                <tagclass>abc.MyTag</tagclass>

                </tag>

</taglib>

Copy this code and paste into mydate.tld file

JSP file

Index.jsp

<%@ taglib uri="/WEB-INF/mydate.tld" prefix="my" %>

<html>

<head>

</head>

<body>

<h3>Current date and time</h3>

<my:date></my:date>

</body>

</html>

Copy this code and paste into index.jsp file

Output
 
For more please write mail to info@bel-technology.com



!----------------------------------------------------Huntertech-------------------------------------------------------!

Team
www.bel-technology.com




 

You Might Also Like

0 comments

Our Logo

Our Logo
Tech Hunters

Like us on Facebook