Friday, June 28, 2019

java - Tag library versus scriptlets in JSP

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
true



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.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...