Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : JSP : Introducing Scripting Variables in JSP Tags
 

We are done with SecondTag.java, our main JSP tag class. On the next page we learn about TagExtraInfo class, we just have to extend it, override one of it's methods and that's it.

TagExtraInfo Class
If you have followed the earlier article then you would have noticed that the only thing different we have done till now for declaring scripting variables in JSP tag is to put following line in our main class's doStartTag() method :

	pc.setAttribute("time", cal.getTime().toString());

One last thing to do to declare scripting variables from a JSP tag is to create a new Java class which extends TagExtraInfo class and overrides one of it's methods; getVariableInfo().

TagExtraInfo class as it's name suggests is an "extra info" class which is *required* when declaring scripting variables from JSP tags. Let us now create our SecondTagTEI class which extends TagExtraInfo and overrides it's getVariableInfo() method.

SecondTagTEI Class
Create a new .java file in the /WEB-INF/com/stardeveloper/tag/test folder. It is the same folder where we kept the SecondTag class. Now copy and paste the following text in it and then save it as SecondTagTEI.java.

package com.stardeveloper.tag.test;

import javax.servlet.jsp.tagext.*;

public class SecondTagTEI extends TagExtraInfo {

	public VariableInfo[] getVariableInfo(TagData data) {
		return new VariableInfo[] {
			new VariableInfo("time", "java.lang.String", 
				true, VariableInfo.NESTED)
		};
	}
}

Explanation
As you can see, our SecondTagTEI class extends TagExtraInfo class and overrides it's getVariableInfo() method. In this method we return an array of VariableInfo classes. Since we have only one scripting variable, time to declare, there is only VariableInfo class returned in the array.

public VariableInfo[] getVariableInfo(TagData data) {
	return new VariableInfo[] {
		new VariableInfo("time", "java.lang.String", 
				true, VariableInfo.NESTED)
	};
}

There are four arguments to the VariableInfo constructor. First is obviously the name of the scripting variable that you want to make available, which in our case is time. Next argument is the type of that variable. Ours is of type String. Third argument is a boolean value asking if this scripting variable needs to be declared? our answer is yes. Fourth and the last one is the scope argument. This scope argument can be one of the following three types :

  • VariableInfo.AT_BEGIN Variable is visible after start tag.
  • VariableInfo.AT_END Variable is visible after end tag.
  • VariableInfo.NESTED Variable is visible between start/end tags.

Now compile this SecondTagTEI class too to get SecondTagTEI.class file. We are done with creating Java class files for our JSP tag. Let's complete this tag by updating the DemoTags.tld ( Tag Library Descriptor ) file we created in the earlier article.

DemoTag.tld
Create a new file ( or use the one created in the first article ) and save it as DemoTags.tld in the /WEB-INF/tlds/ folder. Copy and paste the following text in it :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
	"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 

<taglib> 
	<tlibversion>1.0</tlibversion> 
	<jspversion>1.1</jspversion>
	<shortname>DemoTags</shortname>
	<uri>http://www.stardeveloper.com</uri>
	<info>Demo Tags Library</info>

	<tag> 
		<name>secondtag</name> 
		<tagclass>com.stardeveloper.tag.test.SecondTag</tagclass>
		<teiclass>com.stardeveloper.tag.test.SecondTagTEI</teiclass>
		<bodycontent>JSP</bodycontent>
		<info>Your second JSP Tag</info>
	</tag> 
</taglib>

Remember you can add more than one tag definition in one .TLD file. How? take a look at following file where firsttag tag definition from the first article has been added on top of secondtag definition :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
	"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 

<taglib> 
	<tlibversion>1.0</tlibversion> 
	<jspversion>1.1</jspversion>
	<shortname>DemoTags</shortname>
	<uri>http://www.stardeveloper.com</uri>
	<info>Demo Tags Library</info>

	<tag> 
		<name>firsttag</name> 
		<tagclass>com.stardeveloper.tag.test.FirstTag</tagclass>
		<bodycontent>empty</bodycontent>
		<info>Your first JSP Tag</info> 

		<attribute>
			<name>name</name>
			<required>false</required>
		</attribute>
	</tag>
	<tag> 
		<name>secondtag</name> 
		<tagclass>com.stardeveloper.tag.test.SecondTag</tagclass>
		<teiclass>com.stardeveloper.tag.test.SecondTagTEI</teiclass>
		<bodycontent>JSP</bodycontent>
		<info>Your second JSP Tag</info>
	</tag> 
</taglib>

Explanation
To see what each of above tags mean, see the first article. No need to repeat that explanation here.

In short there is a Tag Library Descriptor file above with details of two tags. The second one is the secondtag which we have built in this tutorial.

The only thing different you get in the .TLD definition for secondtag is that there is also a tag <teiclass />. This tag is *required* when you are declaring scripting variables from your JSP tags. It's value is the complete address of your class which extends TagExtraInfo class, which in our case is SecondTagTEI class.

Ok we are done with DemoTags.tld file.

SecondTag.jsp JSP page
Create a new .jsp file and copy/paste the following code in it. Then save it as SecondTag.jsp in whatever folder your application server can access. For example's purpose I assume you placed it in /web/jsp folder.

<html>
<head>
	<title>Your second JSP tag : SecondTag</title>
	<style>
	p, b { font-family:Tahoma,Sans-Serif; font-size:10pt; }
	b { font-weight:bold; }
	</style>
</head>
<body>

<p align="center"><em><u>Your second JSP tag : 
SecondTag</u></em></p>

<%@ taglib uri="/WEB-INF/tlds/DemoTags.tld" prefix="star" %>
<star:secondtag>
	<p align="center">Date value retrieved from JSP Tag : 
		<%= time %></p>
</star:secondtag>

</body>
</html>

Explanation
We use JSP taglib directive to import the DemoTags.tld tag library in our JSP page. The value of uri attribute in the taglib directive is the complete local address to the .TLD file ( DemoTags.tld ). The value of prefix attribute can be anything, I chose "star".

<%@ taglib uri="/WEB-INF/tlds/DemoTags.tld" prefix="star" %>

We are done with SecondTag.java, our main JSP tag class. On the next page we learn about TagExtraInfo class, we just have to extend it, override one of it's methods and that's it.

TagExtraInfo Class
If you have followed the earlier article then you would have noticed that the only thing different we have done till now for declaring scripting variables in JSP tag is to put following line in our main class's doStartTag() method :

	pc.setAttribute("time", cal.getTime().toString());

One last thing to do to declare scripting variables from a JSP tag is to create a new Java class which extends TagExtraInfo class and overrides one of it's methods; getVariableInfo().

TagExtraInfo class as it's name suggests is an "extra info" class which is *required* when declaring scripting variables from JSP tags. Let us now create our SecondTagTEI class which extends TagExtraInfo and overrides it's getVariableInfo() method.

SecondTagTEI Class
Create a new .java file in the /WEB-INF/com/stardeveloper/tag/test folder. It is the same folder where we kept the SecondTag class. Now copy and paste the following text in it and then save it as SecondTagTEI.java.

package com.stardeveloper.tag.test;

import javax.servlet.jsp.tagext.*;

public class SecondTagTEI extends TagExtraInfo {

	public VariableInfo[] getVariableInfo(TagData data) {
		return new VariableInfo[] {
			new VariableInfo("time", "java.lang.String", 
				true, VariableInfo.NESTED)
		};
	}
}

Explanation
As you can see, our SecondTagTEI class extends TagExtraInfo class and overrides it's getVariableInfo() method. In this method we return an array of VariableInfo classes. Since we have only one scripting variable, time to declare, there is only VariableInfo class returned in the array.

public VariableInfo[] getVariableInfo(TagData data) {
	return new VariableInfo[] {
		new VariableInfo("time", "java.lang.String", 
				true, VariableInfo.NESTED)
	};
}

There are four arguments to the VariableInfo constructor. First is obviously the name of the scripting variable that you want to make available, which in our case is time. Next argument is the type of that variable. Ours is of type String. Third argument is a boolean value asking if this scripting variable needs to be declared? our answer is yes. Fourth and the last one is the scope argument. This scope argument can be one of the following three types :

  • VariableInfo.AT_BEGIN Variable is visible after start tag.
  • VariableInfo.AT_END Variable is visible after end tag.
  • VariableInfo.NESTED Variable is visible between start/end tags.

Now compile this SecondTagTEI class too to get SecondTagTEI.class file. We are done with creating Java class files for our JSP tag. Let's complete this tag by updating the DemoTags.tld ( Tag Library Descriptor ) file we created in the earlier article.

DemoTag.tld
Create a new file ( or use the one created in the first article ) and save it as DemoTags.tld in the /WEB-INF/tlds/ folder. Copy and paste the following text in it :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
	"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 

<taglib> 
	<tlibversion>1.0</tlibversion> 
	<jspversion>1.1</jspversion>
	<shortname>DemoTags</shortname>
	<uri>http://www.stardeveloper.com</uri>
	<info>Demo Tags Library</info>

	<tag> 
		<name>secondtag</name> 
		<tagclass>com.stardeveloper.tag.test.SecondTag</tagclass>
		<teiclass>com.stardeveloper.tag.test.SecondTagTEI</teiclass>
		<bodycontent>JSP</bodycontent>
		<info>Your second JSP Tag</info>
	</tag> 
</taglib>

Remember you can add more than one tag definition in one .TLD file. How? take a look at following file where firsttag tag definition from the first article has been added on top of secondtag definition :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
	"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 

<taglib> 
	<tlibversion>1.0</tlibversion> 
	<jspversion>1.1</jspversion>
	<shortname>DemoTags</shortname>
	<uri>http://www.stardeveloper.com</uri>
	<info>Demo Tags Library</info>

	<tag> 
		<name>firsttag</name> 
		<tagclass>com.stardeveloper.tag.test.FirstTag</tagclass>
		<bodycontent>empty</bodycontent>
		<info>Your first JSP Tag</info> 

		<attribute>
			<name>name</name>
			<required>false</required>
		</attribute>
	</tag>
	<tag> 
		<name>secondtag</name> 
		<tagclass>com.stardeveloper.tag.test.SecondTag</tagclass>
		<teiclass>com.stardeveloper.tag.test.SecondTagTEI</teiclass>
		<bodycontent>JSP</bodycontent>
		<info>Your second JSP Tag</info>
	</tag> 
</taglib>

Explanation
To see what each of above tags mean, see the first article. No need to repeat that explanation here.

In short there is a Tag Library Descriptor file above with details of two tags. The second one is the secondtag which we have built in this tutorial.

The only thing different you get in the .TLD definition for secondtag is that there is also a tag <teiclass />. This tag is *required* when you are declaring scripting variables from your JSP tags. It's value is the complete address of your class which extends TagExtraInfo class, which in our case is SecondTagTEI class.

Ok we are done with DemoTags.tld file.

SecondTag.jsp JSP page
Create a new .jsp file and copy/paste the following code in it. Then save it as SecondTag.jsp in whatever folder your application server can access. For example's purpose I assume you placed it in /web/jsp folder.

<html>
<head>
	<title>Your second JSP tag : SecondTag</title>
	<style>
	p, b { font-family:Tahoma,Sans-Serif; font-size:10pt; }
	b { font-weight:bold; }
	</style>
</head>
<body>

<p align="center"><em><u>Your second JSP tag : 
SecondTag</u></em></p>

<%@ taglib uri="/WEB-INF/tlds/DemoTags.tld" prefix="star" %>
<star:secondtag>
	<p align="center">Date value retrieved from JSP Tag : 
		<%= time %></p>
</star:secondtag>

</body>
</html>

Explanation
We use JSP taglib directive to import the DemoTags.tld tag library in our JSP page. The value of uri attribute in the taglib directive is the complete local address to the .TLD file ( DemoTags.tld ). The value of prefix attribute can be anything, I chose "star".

<%@ taglib uri="/WEB-INF/tlds/DemoTags.tld" prefix="star" %>

Previous ( 1 Gone )( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 2, Comments: 5 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. BAD STRUCTURE ( 3 Replies )
  2. Path to be given

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.