Daniel Rosengarten wrote:

> I have coded both ways and have found tags to be useful when there is a lot
> of validation in the tags or specialized processing.  Otherwise, JSP tags do
> not reduce bugs or system maintainability (over using beans/model2)
>

Wearing my Java developer hat, one major aspect of using custom tags (over
scriptlets) has definitely appealed to me, along with using beans and a Model 2
framework -- and that is code re-use.

Consider a tag that I've built for a form processing taglib (in the jakarta-struts
project at <http://jakarta.apache.org>) that is called like this:

    <struts:text name="username"/>

and which, when included in a JSP page, does all of the following:

* Looks up the bean with which this form field is
  associated, which is established when the
  enclosing <struts:form> tag is started.

* Grabs the current value of the property named
  "username" from this bean, similar to what
  <jsp:getProperty> does.

* Runs an HTML filtering method on the output
  to avoid problems with things like quotes and "<"
  characters.

* Generates an <input type="text" ...> tag that is
  initialized with this value.

* (Future enhancement) optionally renders a
  JavaScript method to perform client side
  validations like "required" or "must be an integer".

Can I reuse this tag?  Sure ... just use it again and again, as many times as
desired.  And, if I later enhance the tag with additional optional capabilities, I
can go back and add the extra attributes as needed to enable these capabilities.  I
can also fundamentally change what it does, if my needs change, and only have to
modify code in a single place.

Can I do the same logic with a scriptlet?  Sure ... just render all of the Java
logic needed to do the same steps.

What about code re-use?  That's where it gets a little tougher doing scriptlets.
The "usual" way to re-use code patterns like this is to cut and paste, and then
change the details that vary (like the name of the property to grab, and whether or
not to render the validation function).  If you need to make a global change to the
logic implemented by this pattern, you've got a lot of typing and debugging ahead
of you ...

OK, I will just make all the common functions available as a method in a utility
class and call it in my scriptlet!  Well, isn't that encapsulation pretty much
exactly what a custom tag does?  :-)

>
> Just my $0.02
> Daniel

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to