I am relatively new to JSP/Servlet and was doing some intro readings on the topic...but cannot find an answer to a specific question.
I will give an example of what I am doing to give you a good picture.
I am developing custom tags for my employer,
say i have two custom tags <foo> and <bar>. Each of which is properly set in the TLD file and have a java class to handle it using SimpleTagSupport.
currently for testing, i am simply spitting back the body of the tag into the JSP page and it's working super fine for me, like this:
The same code is for the BarTag <bar>.
However, what i want to do is this: set an attribute in the, say, BarTag and be able to access it in the FooTag class. something like this
insided the doTag() of BarTag i will put:
and inside doTag of FooTag i will put
I tried so many tricks and things that I really cannot remember what I tried and i keep getting NULL values. Can someone show me a nice and quick way to achieve the above.
thanks in advance.
I get what you are saying, and yes i have reason to have these tags tightly coupled.
I already tried setting attributes at page and even application scope, like so:
this code i put it in application listener class in the contextInitialized method, but still not working for me
i put this in the doTag() of one of the tag, but still not working:
I even tried this where i explicitly state the scope, but still nothing.
i even tried the getJspContext().findAttribute("test") method which according to the documentation:
public abstract Object findAttribute(String name)
Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.
but still nothing.....
am i missing on some fundamental understanding of how these attributes are being used/set???
can someone post an example if they feel it can help...
If it's about the name, then i changed it.
"A rose by any other name would smell as sweet"
I copied your code into my environment and it works perfectly.
I compared it to mine line by line and it's surprisingly similar...yet i don't know what i am missing in my code that makes it throw a null value.
there is something in my code that my eyes aren't catching....will look into it tomorrow.
anyways man, thanks a lot for the time and effort. At least now i know i wasn't doing something fundamentally wrong
i am setting the attribute in a tag invoked in a page called Testing.jsp
and trying to retrieve that attribute in another page called linked.jsp.
and since we are setting the attribute at the page scope, it seems now reasonable that linked.jsp couldn't find the attribute, because it was set in the Testing.jsp page....ie, different page scopes.
correct me if i am wrong.
however, if that's the case, shouldn't we be able to set some attributes at the application level (or scope)....an attribute that all different page scopes can see??
If you need the data to be available across multiple requests, session scope would be appropriate.
I second that
To make it little bit clear
Page Scope -> when it needs to be accessed from "Same JSP Page"
Request Scope -> when need to be accessed across diff parts of web app, that the "request traverses"
Session Scope -> when need to be accessed from any where but all requests should be from the "Same user session"
Application Scope -> when need to be accessible from anywhere by "anyone"