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

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.

MyTEI.java

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");
  pcObject.setAttribute("Heading",head);
  return EVAL_BODY_INCLUDE;
  }

  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.

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

"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" %>
<html>
 <head>
  <title> Scripting Variable in JSP</title>
   <style>
  h1 { font-family:Comic Sans MS; font-size:15pt; }
   </style>
  </head>
<body>
   <sv:svtag>
   <h1>Tag Created by :<%= Heading %></h1>
   </sv:svtag>
  <br><hr>
  You have created a tag<br>
   using Scripting Variable.
</body>
</html>

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

<taglib> 
   <tlibversion>1.0</tlibversion> 
   <jspversion>1.1</jspversion>
   <shortname>sv</shortname>
   <info>Scripting Variable Tag</info>
  <tag> 
  <name>svtag</name> 
  <tagclass>tags.SVTag</tagclass>
  <teiclass>tags.MyTEI</teiclass>
  <bodycontent>JSP</bodycontent>
  <info>Your JSP Tag</info>
   </tag> 
</taglib>

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"

Output:

Type the following URL into address bar:

This will show following output on browser:

Download Source code