Custom Tag lib life-cycle question.

D

Daniel Pitts

So, I've created a tag which takes an optional attribute. It seems to
be retaining the value of the attribute between invocations of the tag.


--- CUT -- MyTag.java -- CUT ---
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;


public class MyTag extends TagSupport {
private Object myArgument;
private static int counter = 0;
public int doStartTag() throws JspException {
if (myArgument == null) {
myArgument = new Integer(++counter);
}
try {
pageContext.getOut().print(myArgument);
} catch (Exception e) {
throw new JspException(e);
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return EVAL_PAGE;
}

public Object getMyArgument() {
return myArgument;
}

public void setMyArgument(Object myArgument) {
this.myArgument = myArgument;
}
}
--- END -- MyTag.java -- END ---

In my JSP I have:
<mytags:myTag />
<mytags:myTag />
<mytags:myTag myArgument="help" />

I would expect the output to be
1
2
help
but the output is
1
1
help

My question is, where is the appropriate location to reset the
myArgument reference? at doEndTag? release? somewhere else?

Or, am I going about this the wrong way altogether? Keeping in mind
this is a simplified version of my actual usecase.

Thanks,
Daniel.

(x-posted to comp.lang.java.(programmer/help/gui), follow-up to
programmer)
 
S

Sunny

Hi Daniel,

Everytime you the tag is executed the , counter variable will be
reinitialized in your program.AFAIK, if u want to
your output to be:
1
2
help

then you need to put ur variable in a scope that makes it availabe to
the end of the page from the point of initialisation.It is done in .tld
file.

Regards
Sunil.
 
D

Daniel Pitts

Sunny said:
Hi Daniel,

Everytime you the tag is executed the , counter variable will be
reinitialized in your program.AFAIK, if u want to
your output to be:
1
2
help

then you need to put ur variable in a scope that makes it availabe to
the end of the page from the point of initialisation.It is done in .tld
file.

Regards
Sunil.

First, thanks for your reply.
Second, please don't top post, it makes it hard to read.

Third, my counter variable is static, so unless the whole class is
reloaded, the counter won't be reinitialized. In any case, this was a
simplified example, the new Integer(++counter), would be replaced by
obtaining a variable from an ancestor of the tag.

The problem is that myArgument doesn't get reset to null, and I don't
know why. Where should I reset myArgument?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,146
Latest member
Vinay KumarNevatia_
Top