wood burning stoves
The moose likes Servlets and the fly likes Embedding Custom Tag in Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Embedding Custom Tag in Java Code" Watch "Embedding Custom Tag in Java Code" New topic

Embedding Custom Tag in Java Code

John Zimmerman

Joined: Jun 15, 2001
Posts: 2
I have a JSP page that uses custom tags and I want to pass the value of the custom tag to a bean. It doesn't seem possible and I wondered if anyone has a solution to this problem. For discussion-sake, assume I have a custom tag called ZipCode which returns the current zip code of the user. Assume also that I have a bean which looks up the city name based on zip code. So the code in the JSP would be
<% String cityName = bean.getCityName(<tag:ZipCode/> ); %>
Of course this doesn't work because the java code gets compiled BEFORE the custom tag is resolved. Any ideas or workarounds on how to do this?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Your custom tag could attach some value to the pageContext object with setAttribute() and later code could retrieve the value. As I recall, your custom tag automatically has a pageContext.

author of:
chris leonardi

Joined: Feb 25, 2001
Posts: 3
Hi John,
You might look into using creating a TagExtraInfo class that allows you to set a scripting variable from the ZipCode tag extension that's accessible to your page (and ultimately, your bean, I believe).
I like William's idea, too - I use that technique a lot with tag extensions, as it affords some flexibility in using and manipulating the tag extension's 'return value'. In fact, the Jakarta Struts framework employs this approach for enabling communication between beans and tag extensions.
Hope that helps,
I agree. Here's the link: http://aspose.com/file-tools
subject: Embedding Custom Tag in Java Code
It's not a secret anymore!