• Post Reply Bookmark Topic Watch Topic
  • New Topic

setting attributes to be accessed by cutom tag code  RSS feed

 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Linker Winker", please check your private messages for an important administrative matter.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put the data into page-scoped variables and easily retrieve them.

Architecturally, it sounds like you are heading into dangerous, tightly-coupled territory. Be sure that it makes sense for these tags to be so closely coupled.
 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, thanks for your timely response

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

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the private message I sent you?
 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I just read it.
If it's about the name, then i changed it.

"A rose by any other name would smell as sweet"
 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done the same, but i got the result. You may be missing to add the tag descriptions in TLD's or something else... Anywayz.. here is my sample code that worked on Tomcat.

JSP Page:


TLD file


TagHandlers:SourceTag.java



DestinationTag.java


Cheers!!!
 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth, thanks for the reply

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

thanks again,
 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i just figured out what's 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??

regards,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need the data to be available across multiple requests, session scope would be appropriate.
 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for sharing the knowledge and tips.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!