Introducing Scripting Variables in JSP Tagsby Faisal Khan.
Overview
This tutorial builds on the "
Building Your first custom JSP Tag" article. In that article we learned that JSP
tags are simple Java classes with attributes as properties and must implement Tag
or BodyTag interface. In this tutorial we will
learn what are scripting variables and how to make use of them in JSP tags.
What are Scripting Variables ?
Well scripting variables are variables declared in any scripting language. But when
we talk of them in context of JSP pages we mean the page level variables declared
by JSP tags. To illustrate my point, take a look at the following JSP tag :
<star:secondtag>
<p align="center">Date value retrieved from JSP Tag : <%= time %></p>
</star:secondtag>
Above is a JSP tag whose body content contains simple HTML tags along with a single
scripting variable i.e. time. Isn't it nice that the tag makes us available
scripting variable which we can use anywhere in our JSP page?
Whether you
get the point or not this technique of declaring scripting variables by JSP tags and
then using them on the JSP page is very useful. In this tutorial I'll describe making
use of these scripting variables with a simple example. But the true potential of
scripting variables will only be realized when you read my forthcoming article on
iterating JSP tags which iterate and display content from database. Those JSP tags will
make different scripting variables available to the JSP page to be placed anywhere we like. To
make that use of scripting variables, it is important that you read this article
carefully.
Following are the topics we will cover in this tutorial :
SecondTag JSP Tag
Create a new .java source file in the /WEB-INF/classes/com/stardeveloper/tag/test folder
of your web application. Copy the following text in it and save it as SecondTag.java.
package com.stardeveloper.tag.test;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public final class SecondTag implements Tag {
private PageContext pc = null;
public void setPageContext(PageContext p) {
pc = p;
}
public void setParent(Tag t) {}
public Tag getParent() { return null; }
public int doStartTag() throws JspException {
Calendar cal = Calendar.getInstance();
pc.setAttribute("time", cal.getTime().toString());
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public void release() {
pc = null;
}
}
Now compile SecondTag.java and SecondTag.class file will be created.
Explanation of SecondTag.java Source Code
First line is the package statement, telling that SecondTag belongs to com.stardeveloper.tag.test
package.
package com.stardeveloper.tag.test;
Then we import the packages we will be using in this class.
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
Then comes our class declaration. Notice the "final" keyword? it is not required
for creating JSP tags, I used it because final classes are more efficient than
non-final classes. Our class implements Tag interface. Like I said in the
"Building Your first custom JSP Tag" article,
our JSP class must implement Tag or BodyTag interface. We chose Tag becaues it is
simpler and does the job we are looking for.
public final class SecondTag implements Tag {
Then we declare the sole class-level variable for our SecondTag Java class.
private PageContext pc = null;
As we learned in the last article, we have to implement all of the six methods of
Tag interface which are :
- setPageContext(PageContext p)
- setParent(Tag t)
- getParent()
- doStartTag()
- doEndTag()
- release()
I have listed the methods above in the order they are called by the JSP page. Let us
see how we implement all of them in our SecondTag class.
setPageContext() is pretty simple so we set the private class variable to the
provided PageContext object. Remember that this PageContext object is provided by the
JSP tag and this is the first method which is called by JSP page, thus making us
PageContext object available to be used by later methods if we want.
public void setPageContext(PageContext p) {
pc = p;
}
setParent() and getParent() are for setting the parent Tag reference. A parent tag
is a JSP tag which is enclosing the current tag. Since in SecondTag there is no parent
tag enclosing it, we don't care about these methods and implement the minimum possible
for them.
public void setParent(Tag t) {}
public Tag getParent() { return null; }
Now doStartTag() is the real useful method we should talk about. This method is
called when PageContext and parent Tag objects have been set. The whole point of this
tutorial is to describe how to declare a scripting variable from the JSP tag. To illustrate
that we will declare a single scripting variable called time.
To do that we create an instance of Calendar object and use it's getTime() method
to get Date object. Calling toString() method of retrieved Date object gives us current
date and time. This current date and time will be the value of our time
variable.
To make this scripting variable available to the JSP page we have to make use of
setAttribute() method of PageContext object. Since we already have a private reference
to PageContext object for the current JSP page, we use it's setAttribute() method to
set the value of time variable to current date and time we calculated above.
Once done, we return EVAL_BODY_INCLUDE to tell the JSP page to show the body content
of the JSP tag. Other possible return value is SKIP_BODY which causes JSP page to skip
the body content of the tag, the body content will not be shown to the user. But we
don't need that here.
Remember body content of a tag is the content present between it's starting and ending
tags.
public int doStartTag() throws JspException {
Calendar cal = Calendar.getInstance();
pc.setAttribute("time", cal.getTime().toString());
return EVAL_BODY_INCLUDE;
}
Another useful method in Tag interface is doEndTag(). There isn't much we want to
do in this method so we simply return an EVAL_PAGE value indicating to the application server
to continue evaluating the JSP page. Other legal value is SKIP_PAGE, which will cause
the JSP page to skip further processing of the page, but that we don't want here.
In the last comes release(). The only thing to do here is to release the reference to
PageContext object.
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public void release() {
pc = null;
} Overview
This tutorial builds on the "
Building Your first custom JSP Tag" article. In that article we learned that JSP
tags are simple Java classes with attributes as properties and must implement Tag
or BodyTag interface. In this tutorial we will
learn what are scripting variables and how to make use of them in JSP tags.
What are Scripting Variables ?
Well scripting variables are variables declared in any scripting language. But when
we talk of them in context of JSP pages we mean the page level variables declared
by JSP tags. To illustrate my point, take a look at the following JSP tag :
<star:secondtag>
<p align="center">Date value retrieved from JSP Tag : <%= time %></p>
</star:secondtag>
Above is a JSP tag whose body content contains simple HTML tags along with a single
scripting variable i.e. time. Isn't it nice that the tag makes us available
scripting variable which we can use anywhere in our JSP page?
Whether you
get the point or not this technique of declaring scripting variables by JSP tags and
then using them on the JSP page is very useful. In this tutorial I'll describe making
use of these scripting variables with a simple example. But the true potential of
scripting variables will only be realized when you read my forthcoming article on
iterating JSP tags which iterate and display content from database. Those JSP tags will
make different scripting variables available to the JSP page to be placed anywhere we like. To
make that use of scripting variables, it is important that you read this article
carefully.
Following are the topics we will cover in this tutorial :
SecondTag JSP Tag
Create a new .java source file in the /WEB-INF/classes/com/stardeveloper/tag/test folder
of your web application. Copy the following text in it and save it as SecondTag.java.
package com.stardeveloper.tag.test;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public final class SecondTag implements Tag {
private PageContext pc = null;
public void setPageContext(PageContext p) {
pc = p;
}
public void setParent(Tag t) {}
public Tag getParent() { return null; }
public int doStartTag() throws JspException {
Calendar cal = Calendar.getInstance();
pc.setAttribute("time", cal.getTime().toString());
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public void release() {
pc = null;
}
}
Now compile SecondTag.java and SecondTag.class file will be created.
Explanation of SecondTag.java Source Code
First line is the package statement, telling that SecondTag belongs to com.stardeveloper.tag.test
package.
package com.stardeveloper.tag.test;
Then we import the packages we will be using in this class.
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
Then comes our class declaration. Notice the "final" keyword? it is not required
for creating JSP tags, I used it because final classes are more efficient than
non-final classes. Our class implements Tag interface. Like I said in the
"Building Your first custom JSP Tag" article,
our JSP class must implement Tag or BodyTag interface. We chose Tag becaues it is
simpler and does the job we are looking for.
public final class SecondTag implements Tag {
Then we declare the sole class-level variable for our SecondTag Java class.
private PageContext pc = null;
As we learned in the last article, we have to implement all of the six methods of
Tag interface which are :
- setPageContext(PageContext p)
- setParent(Tag t)
- getParent()
- doStartTag()
- doEndTag()
- release()
I have listed the methods above in the order they are called by the JSP page. Let us
see how we implement all of them in our SecondTag class.
setPageContext() is pretty simple so we set the private class variable to the
provided PageContext object. Remember that this PageContext object is provided by the
JSP tag and this is the first method which is called by JSP page, thus making us
PageContext object available to be used by later methods if we want.
public void setPageContext(PageContext p) {
pc = p;
}
setParent() and getParent() are for setting the parent Tag reference. A parent tag
is a JSP tag which is enclosing the current tag. Since in SecondTag there is no parent
tag enclosing it, we don't care about these methods and implement the minimum possible
for them.
public void setParent(Tag t) {}
public Tag getParent() { return null; }
Now doStartTag() is the real useful method we should talk about. This method is
called when PageContext and parent Tag objects have been set. The whole point of this
tutorial is to describe how to declare a scripting variable from the JSP tag. To illustrate
that we will declare a single scripting variable called time.
To do that we create an instance of Calendar object and use it's getTime() method
to get Date object. Calling toString() method of retrieved Date object gives us current
date and time. This current date and time will be the value of our time
variable.
To make this scripting variable available to the JSP page we have to make use of
setAttribute() method of PageContext object. Since we already have a private reference
to PageContext object for the current JSP page, we use it's setAttribute() method to
set the value of time variable to current date and time we calculated above.
Once done, we return EVAL_BODY_INCLUDE to tell the JSP page to show the body content
of the JSP tag. Other possible return value is SKIP_BODY which causes JSP page to skip
the body content of the tag, the body content will not be shown to the user. But we
don't need that here.
Remember body content of a tag is the content present between it's starting and ending
tags.
public int doStartTag() throws JspException {
Calendar cal = Calendar.getInstance();
pc.setAttribute("time", cal.getTime().toString());
return EVAL_BODY_INCLUDE;
}
Another useful method in Tag interface is doEndTag(). There isn't much we want to
do in this method so we simply return an EVAL_PAGE value indicating to the application server
to continue evaluating the JSP page. Other legal value is SKIP_PAGE, which will cause
the JSP page to skip further processing of the page, but that we don't want here.
In the last comes release(). The only thing to do here is to release the reference to
PageContext object.
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public void release() {
pc = null;
}
|