Are scriptlets in JSPs really that bad?
This post will probably only make sense if you have worked with Java and JSP. Just a warning!
It seems to be commonly accepted that putting code in your JSP pages is a bad thing, at least since the “Model II” architecture (based on the MVC design pattern) and the JSTL tag library were introduced. JSTL encapsulates the common pieces of logic that you usually need in your pages, like iteration and if/else, as well as giving you the ability to format dates, insert localized messages, and output dynamic data. I have fully embraced this “no code in the JSPs” mindset. My JSPs from the past couple of years have not a scriptlet in sight. All the business logic and control logic are safely tucked away in Java beans and servlets, while all the presentation is in the JSPs.
But I’ve been wondering, is it really so awful to use scriptlets in your JSPs? I’ve never ever worked on an application where there is a seperate HTML developer with no knowledge of programming who is going to go muck around in the JSPs and would be scared by a few scriptlets. I really wonder how often this is the case, really. I would think it might not be uncommon to have an HTML developer that hands off an HTML mock-up to the development team, but to actually go in the source and change it themselves? Seems unlikely.
Certainly it does make your JSP more uniform-looking, to have everything be so very XML-like. With scriptlets, there’s always the question of formatting: should it be indented along with the HTML or should it have it’s own indentation “context” (I prefer the latter, myself)? Should the <% and %> be on their own lines? Should there be a space between the <%= and the expression? Cosmetic issues, maybe, but they really can drive a person nuts. There are countless religious wars amongst developers over code style. Take scriptlets out of the equation, and the question of formatting becomes simple.
But sometimes the JSTL just isn’t good enough. For one thing, the expression language it uses takes away the ability to catch errors on compilation. And for those, like me, who use an advanced IDE (like IntelliJ IDEA), that catches errors as you type and provides command-completion, we lose some of our best productivity-boosters. Even worse than that, the JSTL expression language doesn’t provide any way of referencing static constants! Sure, you could use a work-around, like bulking up your Java code with non-static getters and setters for those static constants, or even using introspection to get the same effect, but we shouldn’t have to resort to such measures.
So while I will remain committed to clean MVC design, and to minimizing the code in my JSPs, I have come to the conclusion that the occasional use of scriptlets in JSP is acceptable, so long as they’re limited to strictly “view”-level logic. Maybe someday the JSTL expression language will support static fields, Java Collections will have a java-bean-style property for getting the size, and IDEs will know how to parse expression language so we can spot errors quickly and use command-completion. I’m looking forward to it.
August 24th, 2004 at 1:06 pm
Half the stuff you mentioned I am hardly familiar with - but I do agree that the mere mention of putting scriplets into the JSP page seems to be a horrible sin.
In all the books and resources I’ve read regarding JavaServer Pages, they all the same thing, which is “Don’t put Java code in the JSP! You might confuse the HTML programmer!”
But, as far as your question goes, I don’t think there’s anything wrong with doing it. Depends on the situation. I think all the warnings simply mean, “Don’t get in the habit of doing it.”
On another thought…
When the size and scope of the project increases, you don’t want a bad habit to slow things down.
And then, of course, working with Java and JSP is always mentioned as a “team initiative.” Meaning there is always a large team working on the project, and if one JSP developer is mindlessly inserting Java code into the JSP, then he/she disrupts the “team cycle.” Because, obviously, that is not his/her duty.
I think that’s what all the warnings could be for.
July 1st, 2005 at 11:30 am
Good article. I found your site via google when I was searching around on the topic of using static variables in JSP/JSTL. I happen to work on a project where the HTML developer knows little about programming and scriptlets DO scare him. Also, scriptlets tend to make my jsps very unreadable compared to the JSPS when i use JSTL/Struts tags. You mentioned the lack of “as you type” editor support as a reason to like scriptlets. I haven’t used IntelliJ as of yet, but my editor combination of Eclipse + Nitrox JSP editor does very well. The Nitrox plugin does a lot of nice code completion and as you type checking for validating that your tags “look” correct and adhere to the TLDs. True there is no type-checking, but that is one of the BENEFITS of EL, not having to cast everything from one type to another.
Another benefit of staying away from scriptlets is because scriptlets don’t encourage the developer to design re-usable code. It takes more time up-front to design a good library of custom tags for your application, but once you do it makes modifying and implementing new features a lot faster. Also, if you want to change some of the functionality of your custom tags, you only have to do so in 1 place, whereas if you want to change how some re-used scriptlet code works, you have to update each page where it us used. This is bug-prone in large applications.
I really like the layout of your site Jennifer. I plan on designing mine in a similar fashion with pictures of my sweeter than honey twin boys and the occasional article on whatever topics come to mind.
Contact me sometime if you want to chat about anything:
aim: eadyjava
Y!: stephene
I will setup a link to your site on mine as soon as I get a chance to work on it some more.
July 1st, 2005 at 1:12 pm
Hi Steve, thanks for stopping by.
Interesting to hear about your project, with the roles. Do custom tags bother your HTML developer at all, or just scriptlets?
IDEA does code completion for the custom tags themselves, but not for the expression language, which is where I end up making mistakes.
Have you used XSLT at all? There’s a bit of a learning curve, and there are some limitations which can be frustrating, but it’s kind of nice once you get used to it - now I find myself wanting to use XSLT constructs in my JSP code!
July 1st, 2005 at 1:44 pm
By the way, I just recently discovered the JSTL functions. I’m not sure how long they’ve been around, but they’re useful for taking up some of the slack. Now in your expressions, you can use length() to get the size of collections. There are also some string functions that might or might not be useful to people.
August 8th, 2005 at 12:33 pm
I try to use JSTL in place of struts tag since JSTL is “standard” so to speak. In fact, the only struts package I ever use is HTML. The logic and bean packages have JSTL equivalents. I don’t have any experience in XSLT/XHTML as of yet, however I’m on the verge of learning because lately I’ve been branching out into the world of PHP (like installing and modifying my own WordPress installation). Some of my experiments in PHP have been with scripts that use the X— translation. I look forward to learning more about each.
Professionaly, I’m stuck in the java/struts world for the foreseeable future. However, I do have a nifty over-ridden architecture because the application I am developing is heavily frame-based, which struts doesn’t like too much. Everything has to go through the session rather than the request because the order in which the frames render isn’t guaranteed. It’s slowly becoming a behemoth and any code tips or tricks that I can read about to make it simpler the better off I’ll be.