The Pragmatic Craftsman :: Simplicity from complexity : by Stanley Kubasek ::

JSP 2.0 Tags

Did you know that you could write a JSP tag without writing a single line of Java code? I didn’t. I was surprised today when I discovered that. I’m going to show you how easy it is. Let’s take an anchor tag (<a href=”">link</a>). If you don’t get to write it very often, you might forget the exact syntax. Why not write a simple tag? This would make things easier.

Here is a simple tag, link.tag:

<%@ attribute name="address" required="true" description="Location"%><%@ attribute name="title" required="false" description="Label"%><a href="${address}">${title}</a>

That’s it! You’ve got yourself a jsp tag. Here is an example, link.jsp, of its actual usage.

<%@ taglib tagdir="/WEB-INF/tags/" prefix="tags"%><html><body><p><tags:link address="http://aol.com" title="aol.com" /></p></body></html>

How about that? That’s one cool feature of JSP. Read some other cool stuff about JSP 2.0 here.

Another cool feature is the use of JSTL, which is a pre-defined tag library for common tasks. What tasks? Access to request parameters, access to beans, logic, and other. If you use JSTL, your JSP pages are without Java code. You use a simple scripting language EL (Expression Language) for that. After all, JSP is a view. It should not have any Java code. Period. If you are considering JSP, or if you want to move to JSP, use JSTL. It will make your life much easier. Plus, you’re not going to fall into the trap of doing most of your business logic inside your page. JSTL gives you a seperation of layers by default.

Here is a link to very good JSP 2.0 article, Faster Development with JavaServer Pages Standard Tag Library (JSTL 1.0), by Qusay H. Mahmoud.

Related Posts

2 Responses to “JSP 2.0 Tags”

  1. rob says:

    That’s pretty slick.

  2. mib says:

    It’s a great pity, that (my)eclipse cannot handle these custom tags ……

Favorite Quote

Topics

Tags

Archive

Currently Reading

Shelfari: Book reviews on your book blog

:: The Pragmatic Craftsman recommends

:: The Pragmatic Craftsman book reviews

Info

© 2001-2012 Stanley Kubasek About me :: Contact me

Me on Twitter

@leszekgruchala Good to hear it's fast. Looking to upgrade to IntelliJ 11 soon. - 2 days agoPutting related classes together is another way of looking at "structuring your code by feature." Makes your code more cohesive. Big + imho - 11 days agoplanetgeek.ch » Structure your code by feature - http://t.co/KcpMBKVg (via @sociablesite) #sociable - 11 days agoTell Congress: Don’t censor the web! http://t.co/ZEkgOAW7 - 18 days agoI must admit, I'm one of those developers that doesn't know too much about WeakReferences in Java. http://t.co/HjW7v9e0 Time to change that. - 19 days ago

»see more

Recent Entries