This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Embedding Custom Tag in Java Code" Watch "Embedding Custom Tag in Java Code" New topic
Author

Embedding Custom Tag in Java Code

John Zimmerman
Greenhorn

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
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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.
Bill

------------------
author of:


Java Resources at www.wbrogden.com
chris leonardi
Greenhorn

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,
Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Embedding Custom Tag in Java Code
 
Similar Threads
Error in JSP : The end tag "</menu:ddmenu" is unbalanced
Some Brainbench JSP Q's.....Opinions required
Spring with Custom Tag
Process JSP Stream explicitly
Doubt in TagSupport (Classic Tag)