Tuesday, December 29, 2009

Custom Tags in Java

It has been a long time I am thinking of learning about the Custom tags in Java.I started reading HeadFirst but was not patient enough to read the full chapter.They specify that now there are different ways to create Custom tags in Java.The old was they name as "Classic".Wht ever it is my purpose is to develop a tag which can populate a drop down.
What I had done for that?
1)Created a java class "Greeter" that extends SimpleTagSupport.
The "SimpleTagSupport " is located in the package --javax.servlet.jsp.tagext.SimpleTagSupport

Now you have to override the doTag ().That is it.Nothing else you have to do in the Greeter class.
Now we should have some mecahanism towrite to the JSP page,which needs this custom tag.
To get the "out" built in variable & write to the JSP page what we have to do?
Simple 2 lines of code..

PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
Now you can write like
out.println("Some thing").
So now we have to construct the "Some thing".
In our example it is a drop down.So you can get the data for poplating the drop down from the DB in the doTag().
To built the combo box... & populate data.


public static final String OPTION_BEGIN = "<option value=\"";
public static final String VALUE_END = "\">";
public static final String OPTION_END = "</option>";

StringBuffer sb=new StringBuffer();
//Here you can fetch data from the DB and do what ever you want.
sb.append("<select>");
sb.append(OPTION_BEGIN);
sb.append("Monday");
sb.append(VALUE_END);
sb.append("Monday");
sb.append(OPTION_END);
sb.append(OPTION_BEGIN);
sb.append("Tuesday");
sb.append(VALUE_END);
sb.append("Tuesday");
sb.append(OPTION_END);
sb.append("</select>");
---------------------------------------------------------------------------
The complete Greeter class:

package com.ustg.customtag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Greeter extends SimpleTagSupport {
public static final String OPTION_BEGIN = "";
public void doTag() throws JspException {
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
try {
StringBuffer sb=new StringBuffer();
/*Here you can fetch data from the DB and do what ever you want.Iterate over a List &
populate the data */
sb.append("");
System.out.println(sb.toString());
out.println(sb.toString());
} catch (Exception e) {
// Ignore.
e.printStackTrace();
}
} }
---------------------------------------------------------------------------
Now we need a tld which form the link between our JSP page & the Greeter class.
FileName:demo.tld
Put it directly inside WEB-INF.



1.0
My Shortname
USTuri

greeter
com.ustg.customtag.Greeter
empty


My tags name is "greeter".
---------------------------------------------------------------------------
The JSP page:
HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="custom" uri="USTuri" %>

<html>
<head>
<meta equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Custom Tag Development</title>
</head>
<body>
<c:out value="My Custom Drop Down "></c:out>
<custom:greeter/>
</body>
</html>

---------------------------------------------------------------------------