Is it good practice to have HTML output from a custom tag? For example, what if our java developers are working with web designers? The hmtl that is output by the tag might not have the corret style applied, etc.
The following quote from this article is a pretty good guidline:
Favor HTML in Java handler classes over Java in JSPs Sometimes cleanly separating HTML, JSP tags, and HTML-like custom tags from Java requires unnecessarily convoluted code. In these cases, you either include Java scriptlets and expressions in the JSP or put some HTML code in the Java tag handler class. I'd rather see a small amount of HTML code in the Java class than see Java, such as scriptlets and expressions, in the JSP. Since custom tag handlers are specific to the custom tags they implement (and not reusable outside JSPs), placing necessary HTML there is not troublesome. Sun's Java 2 Platform, Enterprise Edition (J2EE) Blueprints documentation discusses this issue further.
There are exceptions to this standard: if including one or two lines of Java code as scriptlets in the JSP solves the same problem that would require many more lines of HTML in the Java handler class, allowing Java code to exist in the JSP page might be prudent.
(Sorry if this looks like hijacking the thread, but I thought it was relevant to the original post and the replies to it.)
That article that Ben linked to is from 2001, and JSP has changed a lot since then. I have a similar case that doesn't involve (much) HTML, but does raise the Java-versus-JSP question. We have a "cost" bean that needs to display like "23.47 (1.96 each)" or just "3.27" in some cases, and a retail bean that needs to display like "1.99 (37.3%)" with optional effective date. And these numbers appear in several different places in the application.
Now I originally had that formatting being done by a static method of a helper class written in Java, but while rewriting JSP+scriptlets to JSP+JSTL I found I couldn't make the scriptlet go away. So I changed that Java code to JSTL and used jsp:include to avoid the sin of pasting it into many places.
But I'm not convinced that was the best way to do it. The new all-JSP version is noticeably slower, for one thing, but also from a design standpoint it's a lot easier to write that finicky formatting code in Java. So I'm leaning towards bringing back the Java version and making myself figure out how to call it from JSP+JSTL. Any comments on that?
I find it interesting that you've noticed a performance drop after de-Java-izing your pages. I've never noticed any such issue. Just speculating, but I wonder just how much jsp:include-ing you are doing.
What you describe is exactly the type of usage I would use a custom tag for.
Originally posted by Bear Bibeault: I find it interesting that you've noticed a performance drop after de-Java-izing your pages. I've never noticed any such issue.
Yes, well, it's also true that it's running in my IDE that could do with more memory. And I probably notice it more because JSPs are being compiled. In production it might well turn out to be insignificant.