4

What is the advantage (if there is one) of using a custom action instead of a scriptlet?

For example, which is better in a performance context?

<c:if test="${param.Clear}">
  <font color="#ff0000" size="+2"><strong> 
  You just cleared your shopping cart! 
  </strong><br>&nbsp;<br></font>
</c:if> 

or

<%if (param.Clear){%>
      <font color="#ff0000" size="+2"><strong> 
      You just cleared your shopping cart! 
      </strong><br>&nbsp;<br></font>
<%}%>

I have a big JSP project and I want to know if is necessary to use tag libraries or if I can keep my Java scriptlets.

Also, I want to implement custom tags for some things that now are separate JSP files with Java, JavaScript, CSS, and HTML code.

erickson
  • 265,237
  • 58
  • 395
  • 493
jotapdiez
  • 1,456
  • 13
  • 28

5 Answers5

3

As far as performance goes, they both get compiled down to servlets, so they should perform equally well.

There doesn't seem to be much to recommend JSTL in the example you give, but what happens to projects I've seen is code like:

<%if (param1.Clear() && param2.isSomeFlagSet() && !param3.isSomeOtherFlagSet()){%>
      <font color="#ff0000" size="+2"><strong> 
      You just cleared your shopping cart! 
      </strong><br>&nbsp;<br></font>
<%}%>

and it gets copied all over. >_< If you replace this with JSTL mechanically you can get something just as bad, obviously, but it gives you an opportunity to organize things better.

Follow BalusC's advice and get rid of inline Java code from your JSPs where you can.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
3

I love JSP. I think its the best of the Java page serving markup languages. It's main fault is that it doesn't work well for things like emails and such, since you can't just pass a string and a map to an evaluator and get a result back like you can with something like Velocity.

But beyond that use case, JSP is great.

To answer your question, you can still retain your scriptlet code. Something like a scriptlet "if" vs a c:if tag is certainly going to be faster CPU wise since an "if" is simply an if whereas a c:if is a method call plus some environmental shenanigans when invoking the tag.

That said, it's a different question as to how much of an impact that speed difference makes.

Most of the code shouldn't be in the JSP, obviously, the logic should mostly be in the Servlet or whatever else you're using on your back end, and the JSP contains just rendering code (which can certainly involve IFs and FORs etc.)

The primary argument against scriptlets is JSP Tag Files. Tag files are what separate JSP from the others. JSP Tag files make JSP refactorable, and make adding new tags painless. But the downside is that when using Tag Files, any tags your create can not have scriptlet code within their boundaries.

For example, if you created a "TABLE" tag, anything between the t:table and /t:table elements could not have scriptlet elements. However, you can certainly have scriptlet code within the Tag File implementations, so I just wrap any scriptlet stuff I need in those.

You can go here: JSP tricks to make templating easier? for an overview of Tag Files. But we go far beyond that here. We define forms and tables and components and all sorts of stuff using Tag Files.

Community
  • 1
  • 1
Will Hartung
  • 115,893
  • 19
  • 128
  • 203
1

Here is why I think JSTL is the better choice.

  1. Using JSTL is easier to read.
  2. If you created a method in your JSP, then only that JSP can use it. So reusability is also one.
  3. pageScope and requestScope objects are already readable using ${foo} and not request.getAttribute('foo')

Cons - There are somethings you just can't do in JSTL.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
1

I'd rather get rid of JSP at all... I have worked with JSP now for 6 to 8 years and it is simply not worth using. A template engine gives you a better turn around.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
1

There may be some negligible performance differences between the two options you proposed, however I do not think runtime performance is really the problem that Tag Libraries are intended to solve.

IMHO, the valid arguments against scriptlets are almost all about coding, debugging and maintenance concerns. One of the main reasons JSP has been derided for so long is not so much about JSP, but more about how people have used it. There is nothing worse then opening a JSP (that someone else wrote) in your IDE and seeing this spaghetti code mess of scriptlets and HTML. If you want to mix server side code and HTML all over the place, you might as well go and code in PHP! :)

Using Taglibs will make your JSPs way easier to develop initially, debug quicker and maintain over the long run.

I think scriptlets are such a poor option, that I always like to add something similar to this little snippet to my web.xml files when starting a new project:

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <scripting-invalid>true</scripting-invalid>
  </jsp-property-group>
</jsp-config>

This turns off scriptlet evaluation in the JSPs inside the web app, forcing developers to find better alternatives.

Another big argument for Taglibs is that you will be coding in a more rigorous manner. For example, you will have to handle any exceptions thrown in your code (one way or another) when you write a Taglib. If you write the same code in a scriptlet, you will not be prompted by your IDE or the JSP compiler to wrap the code in a try-catch block. Lazy/unprofessional programmers will probably rejoice at avoiding having to write a few lines of error handling code. Real programmers know from experience that catching exceptions and handling them properly in Java is way easier, cleaner and robust than having JSPs throw exceptions at runtime. Also if you are into JUnit etc. then unit testing taglibs is pretty straightforward - by definition you probably can't really unit test a JSP, at best you might be able to do some kind of integration test.

Also, as someone also mentioned, there is an architectural benefit to using Taglibs. The code design and reuse factor is way in favor of Taglibs. There is no easy way to share scriptlet code that is embedded in a JSP file. So you end up with copy-paste coding all over the place.

Craig S. Dickson
  • 407
  • 6
  • 14