You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by "Nuttall, Chris" <Ch...@intervoice-brite.co.uk> on 2000/07/25 18:19:23 UTC

Tag to replace <% String counter = "counter" %> in JSP...???

Hi,

Is it possible for a custom tag to declare/initialise
variables within the tag handler class, to replace
<% String counter = "counter" %> from within a
JSP page?

Perhaps...

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


/**
 * SetCounterTag adds String variable to page scope.
 */
public class SetCounterTag extends TagSupport {

    public int doStartTag() throws JspException {

	// String to be added
	String counter = "counter";

	pageContext.setAttribute("counter", counter);

	// Continue processing this page
	return (EVAL_PAGE);
    }
}


...which could be included within the JSP as:

<tag:setCounter />

...and then access counter later in the JSP page, or
from within another custom tag...???

Regards,


Chris

Re: Tag to replace <% String counter = "counter" %> in JSP...???

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Nuttall, Chris" wrote:

> Hi,
>
> Is it possible for a custom tag to declare/initialise
> variables within the tag handler class, to replace
> <% String counter = "counter" %> from within a
> JSP page?
>
> Perhaps...
>
> import javax.servlet.jsp.*;
> import javax.servlet.jsp.tagext.*;
>
> /**
>  * SetCounterTag adds String variable to page scope.
>  */
> public class SetCounterTag extends TagSupport {
>
>     public int doStartTag() throws JspException {
>
>         // String to be added
>         String counter = "counter";
>
>         pageContext.setAttribute("counter", counter);
>
>         // Continue processing this page
>         return (EVAL_PAGE);
>     }
> }
>
> ...which could be included within the JSP as:
>
> <tag:setCounter />
>
> ...and then access counter later in the JSP page, or
> from within another custom tag...???
>

Yes, this is possible, as long as you include a "TagExtraInfo" class
along with your Tag class.  TagExtraInfo describes (to the compiler) the
name of the scripting variable you are creating, and when it becomes
visible to the rest of the page (at the beginning of this tag, at the
end of this tag, or only from within the body of this tag).

You also have to do the pageContext.setAttribute() call, as you
described above, to store the value.  All the "TEI" class does is tell
the compiler what the variable name is going to be.

>
> Regards,
>
> Chris

Craig McClanahan