How to develop and use custom tags inside a J2EE application example

J2EE specification allows programmers developing their own tags. By implementing custom tags procedures, programmers can develop custom tags such as <customTags:HeaderLogo>, <customTags:FooterLogo> or <abc:XYZ> inside a J2EE application.

Normally, programmers could have similar import functionality by using SUN’s tags however custom tag development gives more flexibility and provides readable code for  programmers .  However complex tag development is also available in J2EE environment by using tag handlers provided by J2EE specification.  I will post a more detailed example on this topic (tag handlers simple and classic tag handler development) . In this post, I will give an example about coding standard tags.

First of all, you will need to create a tags folder inside WEB-INF folder to prevent unwanted access for your tag files.

And create a LogoTag.tag file inside tags folder with the following content

    Document   : LogoTag
    Created on : 26.Jun.2011, 19:49:48
    Author     : tunatore

<%@tag description=”LogoTags” pageEncoding=”UTF-8″%>

<%– this attribute will take its value from the request object –%>
<%@attribute name=”message”%>

<%– content for the LogoTag–%>
<h2>${message}</h2> </br>
<img src=”images/java_powered_logo.jpg”/> </br>

And create another tag file name DescriptionTag.tag and put it into the tags folder too.

    Document   : DescriptionTag
    Created on : 26.Jun.2011, 20:01:08
    Author     : tunatore

<%@attribute name=”fontSize” required=”true”%>

<%@tag description=”DescriptionTag” body-content=”tagdependent” pageEncoding=”UTF-8″%>

<font size=”${fontSize}“><jsp:doBody/></font>

You should put jsp:doBody tag like the above one for getting a long content from the custom tags’ body.

Lets, create an index.jsp file for using the custom tags developed

    Document   : index
    Created on : 26.Jun.2011, 19:47:51
    Author     : tunatore

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@taglib prefix=”customTags” tagdir=”/WEB-INF/tags”%>
<!DOCTYPE html>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
<% request.setAttribute(“message”, “Custom tag will display this mesage”); %>
<customTags:DescriptionTag fontSize=”12″>
Description TEXT will go here…  </br>  
Line </br>    
By </br>

Lets deploy the web application and run it;

Download Source

2 thoughts on “How to develop and use custom tags inside a J2EE application example

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s