• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP 2.0 Custom Tags and Database

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic