wood burning stoves 2.0*
The moose likes JSP and the fly likes JSP 2.0 Custom Tags and Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP 2.0 Custom Tags and Database" Watch "JSP 2.0 Custom Tags and Database" New topic
Author

JSP 2.0 Custom Tags and Database

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have read a few articles on writing custom tags with JSP 2.0 and I have to say that it does seem a lot easier than the old way. So much in fact I will be writing some.
However, I am wondering a few things. Is it a good idea or bad idea to put database calls in a custom tag? I realize that if you are distributing your tags for others to use, it probably doesn't make sense. But if you are writing a custom app, then I can see a huge benefit in putting database calls inside a custom tag and then using the variable in a custom tag to store results, etc. I am just wondering if anyone else has any thoughts on this.
Also, on an aside, what is the advantage of using a custom tag over a servlet?
Thanks.


GenRocket - Experts at Building Test Data
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Is it a good idea or bad idea to put database calls in a custom tag? I realize that if you are distributing your tags for others to use, it probably doesn't make sense. But if you are writing a custom app, then I can see a huge benefit in putting database calls inside a custom tag and then using the variable in a custom tag to store results, etc. I am just wondering if anyone else has any thoughts on this.
Personally I don't put direct database calls in a tag (or in a scriptlet). This is mainly for testability. I find that code in regular ("POJO") classes and beans is the easiest to test, code in tags is harder, typically requiring something like TagUnit to set up the correct environment. JSPs are harder still. The upshot of this is that I prefer to keep as much of my code in POJOs and beans. This is especially true of database code. I really don't like to try and test two things at once - so I don't like to be forced to test that the database access and the tag invocation are working together.
Also, on an aside, what is the advantage of using a custom tag over a servlet?
Umm. They are quite different things. A custom tag is just the encapsulation of a bit of code in a somewhat reusable way. I typicially create a custom tag to replace clumsy, hard-to-test and/or duplicated "scriptlet" code. Custom tags only make sens in the context of a JSP page. A servlet, on the other hand is a way to attach a chunk of code to an HTTP request. The servlet is equivalent to the whole JSP, not just one tag used in it.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

A custom tag is just the encapsulation of a bit of code in a somewhat reusable way....Custom tags only make sense in the context of a JSP page
Most database queries are in the context of a JSP page. Take BB software for example. Everytime I click on the JSP link I am shown a list of current threads. I think it would make things very easy to have your JSP and have a line like:

I know the syntax is not correct, but you get the idea.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Sure, but the comment you quote was aimed at distinguishing tags from servlets. Servlets definately don't make sense in the context of a JSP page.
So let's consider the implementation of a tag such as you have mentioned.
<mytags:threadlist param='jsp'/>
I'm assuming that you intend the output of the tag to be (for example) a list of the top 10 recent threads in the JSP forum :- a typical operation for a bulletin board such as this one.
I'm sure there is a database query somewhere in the code to back this up, but the question is, where?
It's not in a scriptlet, because you have introduced a tag, and tags usually replace scriptlets.
It could be in the code of the tag class itself. It could be in an object instantiated and used by the tag. It could be in an object created elsewhere and made available to the tag (stored in a context, for example). It could also be in a static method or a Singleton method, but those are not very OO, and have a whole bunch of issues surrounding them, so I'll discount them for the moment.
I suggest that putting the code for the database access in the tag class itself is probably not a good idea. This limits reuse and entwines the testing of the database access in with the testing of the tag operation. A test can fail without giving much information about what has failed.
Either of the other serious options (database-access object created and used by the tag, or database-access object created elsewhere and used by the tag) seem much more reasonable. The database access code can then easily be parameterised and reused in similar but slightly different situations (such as a "todays active threads" page), and can be tested independently, without having to start a real or mock servlet/JSP container to run a tag.
As an extra spin to this, separating out the database code from the tag code also has the advantage that you can add clever stuff such as cacheing later (if you want to) without changing any of code for any of the tags.
Did that make any more sense, or am I answering the wrong question?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

That makes perfect sense Frank. Thanks. So in a way, Database access for a Tag should follow the same guidelines as access from a Servlet in that the Data Object should be seperated into it's own class.
So using a custom tag to show the "top 10 recent threads" is not a bad idea, maybe even a good idea, but actually accessing the database within the tag is bad. I get it. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP 2.0 Custom Tags and Database
 
Similar Threads
How to have output of tag handler processed by struts tag handlers?
How volume 2 will deal with custom tags?
Problems with custom Tag
difference between tag and tld
jsp 2.0 taglib invoke