Taglibs make life easier
Stuck using JSP when you'd rather be using CF or PHP? Want to stop reinventing the wheel? Check out some prebuilt taglibs....
Let me start off by saying that I’m not a Java programmer. I took a class once and hate the language, I find it counter-intuitive and just plain dislike it. But circumstances have forced me to develop a large banking application in JSP (interfacing with servlets and beans and so on that another developer wrote) and XSL, and I’ve had a rather rough time of it. The JSP books we have didn’t help much, and the resources I was able to find online royally suck in comparison to PHP or ColdFusion, the two scripting languages I know well. I had zero desire to learn this nonsense, but we had to interface with the mainframe using websphere, and at the time this started, ColdFusion couldn’t do that, and they weren’t willing to look at PHP.
It gets annoying having to go to someone else to do minor little things that you could do so very simply on any other platform besides JSP. Replace, for example. I shouldn’t have to have someone write me a blasted replace function to include in the page just so I can switch a couple of strings around. And number formatting… all sorts of nonsense. It was a nightmare, and the consultant wasn’t much help.
But something was.
If he’d just sent me a link to this in the first place, life would have been easier. But he didn’t, so I didn’t know about taglibs and what they could do until recently. Or to be more precise, I knew what taglibs were, vaguely, but I didn’t know there was a nice library of them out there on Jakarta, and another even better one at ColdTags.
It’s not quite ColdFusion, but as soon as I loaded a few of those in, I became far more productive with the simple things I needed to do but previously could not.
For anyone out there in a similar situation, I thought I’d share some of this information. Maybe it’ll save someone else the trouble I went through.
Format negative numbers in red:
<num:redNegative>-12345</num:redNegative> (wraps negative numbers with red font tag)
Format decimals with fixed x.xx expandable to xx.xxxxx if values beyond fixed size are non-zero
<num:number format="#0.00### " id="A">25.6789</num:number>
Basic number formatting (strip leading zeroes, for example)
Format phone numbers, SSN numbers, strings
Convert text to lowercase
<str:lowerCase>TEXT ALL CAPS</str:lowerCase>
Capitalize First Letter Of Each Word
<str:capitalize>text lower case</str:capitalize>
<str:capitalize><str:lowerCase>TEXT ALL CAPS</str:lowerCase></str:capitalize>
creates Text All Caps
Note: this isn’t perfect. It won’t work for things like ABC CORP where you’d want ABC to stay in caps… but it’s a good start for cleaning up all-caps stuff coming from a mainframe or other environment that uses all caps.
<dt:format pattern="MM/dd/yyyy"><dt:parse pattern="MMddyyyy">12252004</dt:parse></dt:format>
Dump out request headers
Name: <jsp:getProperty name="loop" property="name"/>
Value: <jsp:getProperty name="loop" property="header"/>
Dump out session attributes
Name: <jsp:getProperty name="loop" property="name"/>
Value: <jsp:getProperty name="loop" property="value"/>
Very basic JSP benchmarking
<benchmark:duration output="TRUE">[some code]</benchmark:duration>
(outputs number of microseconds a piece of code took to process. You can have them inside each other, for example one at the top of the header and bottom of the footer, and another around a single piece of code inside the body of the page. Probably useful mostly for times when you need to say "problem’s not on my end," or just to get an idea of how long your JSPs run for.)
<mt:mail to="[email protected]" from="[email protected]"subject="[Subject]">
<mt:message>[body of message]</mt:message>
(usually used for error handling once an app has gone live and you can’t trust the end user to tell you when they have a problem. There are probably better ways to code email functions if a primary function of your app is to send email)
Perform a database query
Note: this sample code requires you to have a JNDI name, but there are other ways to connect, check the documentation. You have to access fields by their column position rather than column name, though. This is fairly awkward, like trying to do use an array for a queryset instead of an associative array/structure, but it works fine for single results. There may be a less awkward way to do it, but I didn’t find one and since almost all the SQL in this app consists of pulling out a single "pretty" textual description for an obscure mainframe status code, I haven’t had a lot of reason to go hunting for more.
<sql:connection id="IL" jndiName="jdbc/theConnection"/>
<sql:statement id="StatusDescription" conn="IL">
WHERE StatusCode = ‘<%= session.getAttribute("STATUS_CODE") %>’
<%– loop through the rows of your query –%>
<sql:getColumn position="1" to="StatusDescription" scope="request"/>
Get Image Dimensions
I haven’t had to use this one, but it’s an action I’ve needed to use fairly often in both CF and PHP, so I’ll include it here.
<img:getSize sourceFile="/var/home/www/html/images/foo.gif" idWidth="imgWidth" idHeight="imgHeight" />
This will get the size of an image and load width and height into variables. I’ve used the PHP and CF versions of this alot in situations where I didn’t want to actually modify the image size, but wanted to test the dimensions and if they were over a certain size, modify them until the image was within the width and height limits I’d set for the page, without breaking the aspect ratio.
It can also do real resizing and rotating, individually or in a batch process.
That’s all for now…
That’s all for now. I hope this helped someone out there. Maybe I’ll do another one later on XSL… another fun-fun-fun topic where most of your googling will find a bunch of arrogant pricks spouting off the holy w3c specs when you’re just trying to get your damned job done and could someone give me a simple answer please. (And let me take a moment to say Thank you a million times to the wonderful ufies at User Friendly who helped me several times to fix my real world problems without a single pinhead wasting my time telling me that I shouldn’t be working with ampersands or whatever my problem of the moment was.)