File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Displaying alternative text when a tag's body is empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying alternative text when a tag Watch "Displaying alternative text when a tag New topic

Displaying alternative text when a tag's body is empty

Michael Arnett
Ranch Hand

Joined: Mar 22, 2001
Posts: 65
Hi all,
I have a custom tag that does a sql query and displays the results. That works fine. However, I woud like to have an alternate message displayed if there is no results. I think this is easy to handle, but I want to use the standard JSP custom tag method, not some crazy scriptlet or some other exotic method. What is the standardized way of producing alternate text when a tag does not return anything.
Thanks in advance,

Sun Certified Programmer for the Java 2 Platform 1.4
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
How about tossing a counter variable, initialized to 0 beforehand, in your while( loop so it will increment each time the while loop is executed.
This way, once you have exited the loop, you can test that variable to see if it has changed. If so, then you continue because results were returned from the DB. If the variable is still 0, then you exit out and display your suggested text because there were no results which matched your SQL statement.
If that is not the option you were looking for, you could always run two queries. The first being to select the count of the records which would be returned, the next to run the query again returning the actual rows retrieved instead of their count.
Sam Dalton
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
Does the tag have a body already? IF not then try this:
Once you have done your query, check that the rs has data in it, if not then EVAL_BODY_INCLUDE, if it does, display your results and SKIP_BODY at the end.

<a href="" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of []Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
I agree. Here's the link:
subject: Displaying alternative text when a tag's body is empty
It's not a secret anymore!