• Post Reply Bookmark Topic Watch Topic
  • New Topic

HTML in tag and Java in JSP  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This paragraph is from http://www.javaworld.com/javaworld/jw-11-2001/jw-1130-jsp.html:

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.

I am not able to make relation between HTML in tag and Java in JSP???

 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an exception to the rule stated beforehand: Separate HTML from Java

The basic rule is that all HTML should be on the JSP page, and all java code should be in a java class.
ie - on a JSP no scriptlet code <% if (condition) { %> Greetings <% } %>
and in a Java class, no HTML:
out.println("<h1>Greetings</h1>");

But you can't achieve this in all cases.
When you can't maintain a clean separation, then it is saying you should put the HTML into java code for the custom tag handler you have written, rather than java scriptlet on the JSP.

The article is getting a little old now - there have been several advances, the most notable being JSTL and EL, which help remove scriptlet code from the page, and also means that you don't need to write as many custom tags.

Cheers,
evnafets
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well put, Stefan.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!