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.