Home Jsp Scripting Variables in JSP Custom Tag

Related Tutorials


Share on Google+Share on Google+

Scripting Variables in JSP Custom Tag

Scripting variables are variables that are available to the JSP page when any JSP page is called.

Scripting Variables in JSP Custom Tag


Example program to demonstrate use of scripting variable in Custom Tag

What are Scripting Variables ?
Scripting variables are variables that are available to the JSP page when any JSP page is called. Scripting variables may be any scripting variables but when we are dealing in reference of JSP page we mean page level variables declared by the JSP page. You can access scripting variables in scriptlet, declaration, expressions.

Two ways of defining and using scripting variables in custom tag:

  • In JSP 1.1 you have to define variables in a TEI (TagExtraInfo) file, implement TEI class, and then define the scripting variables in the tag handler.
  • In JSP 1.2 you have to define the variables in the tag handler and add variable elements in the TLD file that configures the scripting variables.

Use of scripting variables in JSP 1.2:

This is same as we have developed a custom tag without TEI Class. For more information go to www.roseindia.net/jsp/custom-tag.shtml

Use of scripting variables in JSP 1.1:

Use of Scripting Variables in JSP 1.1 requires to define variables in a TEI file and to do this you have to implement TEI (TagExtraInfo) class. TagExtraInfo class is required when declaring scripting variables.


Create a new MyTEI.java file which is extending TagExtraInfo class and overrides it's getInfoVariable() method.

package tags;
import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.tagext.*;

public class MyTEI extends TagExtraInfo {

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

Here we have returned only one array of VariableInfo class since we are showing "Heading" once in JSP page. So to return this array we have used constructor VariablleInfo() which is having four arguments. First is Scripting Variables name, second is type of that variable and third argument is of type boolean to ask that "Is it required to be declared ?" and fourth argument of constructor is a Symbolic constant which can have three values: 

  1. VariableInfo.AT_BEGIN ( variable is to be visible after "start" tag )
  2. VariableInfo.AT_END ( variable is to be visible after "end" tag)
  3. VariableInfo.NESTED ( variable is to be visible between "start" and "end" tag)

To use this scripting variable in your tag you have to call setAttribute() method. Since we want to show other contents outside these start and end tag so our doStartTag() returns EVAL_BODY_INCLUDE and doEndTag() returns EVAL_PAGE. While implementing Tag interface, we have to override its doStartTag(), doEndTag(), setPageContext(), release(), setParent() and getParent().

Full code of "SVTag.java" is as follows:

package tags;

import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class SVTag implements Tag {

  private PageContext pcObject = null;

  public void setPageContext(PageContext pc) {
  pcObject = pc;
  public int doStartTag() throws JspException {
  String head = new String("Amit");

  public int doEndTag() throws JspException {
  return EVAL_PAGE;

  public void release() {
  pcObject = null;

  public void setParent(Tag t) {}

  public Tag getParent() { return null}

We can use this custom tag as follows.

<h1>Tag Created by :<%= Heading %></h1>

"sv" is tag prefix, tag name is "svtag" and "Heading" is scripting variable.

Full JSP code of ScriptingVarUse.jsp is as given below:

 <%@ taglib uri="/WEB-INF/teitaglib.tld" prefix="sv" %>
  <title> Scripting Variable in JSP</title>
  h1 { font-family:Comic Sans MS; font-size:15pt; }
   <h1>Tag Created by :<%= Heading %></h1>
  You have created a tag<br>
   using Scripting Variable.

To use tag defined by SVTag.java you have to make a ".tld" file which consists of tag's shortname, name, bodycontent, teiclass etc. 

<?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"> 

   <info>Scripting Variable Tag</info>
  <info>Your JSP Tag</info>

To run this example you have to follow these steps :

  • Create and save MyTEI.java
  • Compile MyTEI.java and put MyTEI.class into classes folder of your web application.
  • Create and save SVTag.java
  • Compile and put SVTag.class file into classes folder of your web application.
  • Create teitaglib.tld and put this file into WEB-INF folder.
  • Create ScriptingVarUse.jsp and 
  • Start Tomcat web server and run "ScriptingVarUse.jsp"


Type the following URL into address bar:

This will show following output on browser:

Download Source code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 8, 2008

Related Tutorials

Discuss: Scripting Variables in JSP Custom Tag  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 1, 2011
Custam Tag

<tsa:table> 1 <tsa:tr> 2 <tsa:td> 3 String Str_Var=""; 4 </tsa:td> 5 </tsa:tr> 6 </tsa:table> 7 Str_Var="Value"; tsa is My Custom Tag. I access Str_Var(Line #7) Then Variable Not Found Exception. Please Solution Me.........