File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Return value from within tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Return value from within tag" Watch "Return value from within tag" New topic

Return value from within tag

Ian Beckley

Joined: Apr 13, 2005
Posts: 4
I work for a small software company and the majority of my duties entail writing examples for our help files. I program in VB, C++ and dotNet but we are getting ready to release a java version of one of our products so I have to familiarize myself with the java language. Doing things is servlets is fairly easy, but when using a .jsp page I can't figure out how to return values from within tags. Given the following code how would I, for example, return the value of "text" so that a user could verify it. Can it be done from outside the tag? ...

<fpspread:cell row="5" col="-1">
<fpspread:button text="Click Me" command="MyCommand" buttonType="push" />
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1044
When you say you want to "return" the value of text, do you mean how do you get it to show up in the browser?

In button.doStartTag() put something like...

...or whatever HTML you want.

Inside of the (Java class name == tag name), the identifier "pageContext" refers to the PageContext object that allows you acces to all sorts of stuff, including the output stream that sends data to the user's browser.

Also, for each custom tag attribute, such as "text", in the JSP you'll have a getter and a setter, such as getText() and setText(). The attribute name is automatically "mangled" so that the method names are in what they call "camel case", the first letter lower-case and subsequent words capitalized. An attribute like buttonType would have corresponding getButtonType() and setButtonType() methods.

On the other hand...
If by "return" you mean you want to pass back some value that the JSP can use later, well that gets a little more complicated. You'll need to use the pageContext to get the HttpSession object, set a value in there, and then retrieve that value in your JSP:

In button.doStartTag():

Then in your JSP:

(In the JSP, "session" is a predefined HttpSession variable.)

(edit)That depends on what class your JSP engine uses for JSPs. I'm using BEA Weblogic 6.someodd, so all my JSPs get translated to classes subclassing weblogic.servlet.jsp.JspBase. Worst-case, you'll have to get the HttpSession from the HttpRequest object named "request", which I believe is pretty standard for all JSP->Java class translaters.(/edit)

Is this what you were looking for?

[ April 13, 2005: Message edited by: Ryan McGuire ]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to JSP...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ian Beckley

Joined: Apr 13, 2005
Posts: 4
Ryan -

That's exactly what I'm looking for. Thanks much...
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
and of course you'd use JSTL instead of scriptlets (and probably requestscope instead of sessionscope)

I agree. Here's the link:
subject: Return value from within tag
It's not a secret anymore!