Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tag library versus scriptlets in JSP [duplicate]

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.

like image 359
jotapdiez Avatar asked Jan 18 '23 19:01

jotapdiez


1 Answers

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.

like image 70
Nathan Hughes Avatar answered Jan 30 '23 09:01

Nathan Hughes