JSPs, JSTL and Custom Tags

The falkner book is the best one to follow while learning jsps. Try out some examples with expressions, scriptlets, directives, Declarations. This is the easiest part in J2EE.

JSTL

 The core JSTL tags perform iteration, conditional processing, and expression language support.
Example: <c:forEach>,<c:set> etc 
Access tags provided in this TLD by placing
 < %@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %> at the top of your JSP.
Installation……
1) Download jstl1.0.6 from
http://jakarta.apache.org/site/downloads/downloads_taglibs-standard-1.0.cgi
download 1.0.6.zip
2) unzip it
3) copy standard.jar and jstl.jar from taglibs-home/lib to your application/WEB-INF/lib
4)(In Eclipse) goto properties of ur project->javabuildpath->add jars->select the above 2 jars

Assignments
1)select records from database and print them on the screen using <c:forEach><c:out> .
2) some more programs in jstl
3) try out all examples in falkner book

Custom Tags
It is time to write your own tag library.  Customs Tags concept is very important for building production quality web applications. No need of installing any third party APIs and all for writing custom tags. 
First custom Tag: Display a string(say xxxxxxxxxx) in Phone format (like xxx-xxx-xxxx).
To achive this we need java scriptlets with string manipulation commands on JSP which is moslty not allowed in real time projects. In real web application moslty the jsps are built with tags or Expression Language, no scriptlets, expressions and all.

Create myTags.tld file under WEB-INf folder with below code.
!DOCTYPE taglibPUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN”http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd&#8221;>

<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>myTag</shortname>
<tag>
<name>fmtPhone</name>
<tagclass>com.sample.formatPhone</tagclass>
<bodycontent>JSP</bodycontent> <attribute>
<name>phone</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute> </tag></tag-lib>
Name under tag  indicates the tag name . Ex: forEach, out .. TagClass indicates the Tag handler. body content indicates the tag body content like <my:fmtPhone phone=”xxxx” > some jsp code</my:fmtPhone> . Name under bodycontent indicates Name of the attribute to be passed. required means the attribute is mandatory. rtExprValue means runtime expression value.

create an entry for this tld file in web.xml
<taglib><taglib-uri>WEB-INF\myTags.tld</..><taglib-location>WEB-INf\myTags.tld</…></tag>

Create Tag handler
import javax.servlet.jsp.JspException
import javax.servlet.jsp.tagext.TagSupport;
//This tag is used to display phone number in xxx-xxx-xxxx format
public class formatPhone extends TagSupport{
Private String Phone=””;
public final int doStartTag() throws JspException{
String format=””;
if(phone.length()>2)
{format=”(“+phone.substring(0,3)+”)”;
if(phone.length>=5){
format=format+” “+phone.substring(3,6)+”-“;
if(phone.length>=9){
format=format+phone.substring(6,10);
}}}
else
format=phone;
str.append(format);
try{
pageContext.getOut().write(str.toString());
}catch(Exception e){e.printstacktrace();}}
return EVAL_BODY_INCLUDE;
public String getPhone(){return phone;}
public void setPhone(String s){phone=s;}
}
Finally Use this tag in Jsps like this.
Import tag library into JSP <taglib uri=”WEB_INF\myTags.tld” prefix=”my”/>

Assignments on Custom Tags
1) Display minutes in HH:MM format (say 65 mins=01:05 mins)
2) Display Breadcrumbs on Jsps using custom tags
Do the final assignment attched here https://klavanya.files.wordpress.com/2006/12/assignment21.doc

Leave a comment if any questions..

Published in: on December 21, 2006 at 10:32 pm  Comments (7)  

The URI to TrackBack this entry is: https://klavanya.wordpress.com/2006/12/21/jsps-jstl-and-custom-tags/trackback/

RSS feed for comments on this post.

7 CommentsLeave a comment

  1. very interesting. i’m adding in RSS Reader

  2. Hi, i like ur blog very much, but i have some doubts about j2ee, so how can i contact you, your explnation is very good, my no is : 9949143555

  3. Great Work Lavanya keep it up

  4. hi there,how r u? ur blog is very good.
    hey are you KOMPELLA APARNA FROM DEHRADOON.

  5. Great Work Lavanya keep it up

  6. I’m starting up a blog related to trade show displays. I’m searching for information to add to the site. I found your site by searching for table top displays. Thought you might want to know.🙂

  7. It is a good work. But it would be great if you could elaborate the the API’s a little more. For example,EVAL_BODY_INCLUDE. What does it means? Why do we need to rerurn such a variable? Where the content would be added if I call getOut().write method (in between the open and closing of the tag or somewhere)?

    If you could mentions such basic but informative tips, then it would be a great refernce for begginers.

    Thanks for your time
    Shyam


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: