Monday, April 23, 2007

Move To Scriptless JSP A Pain

If you plan to use 'scriptless JSP' in your Java web applications, it is much easier to start out with the idea than hope to convert existing JavaServer Pages (JSP) after building your application. Scriptless JSP (s-JSP) involves moving all Java code, scriptlets, declarations, and "set operations" away from JSPs to servlets, leavings the JSPs display-oriented. Dynamic content in the JSPs is then provided by a combination of the JSP Expression Language (EL), JavaBeans, JSTL/custom actions, and objects in mainly the session and application scopes.

Scriptless JSP is great for enforcing the MVC design model, allowing non-Java programmers to take control of designing the presentation/view a lot easier. It also conforms to XML specifications by using characters that XML engines won't freak about. For example, the '>' sign is known simply as 'gt' in EL - the day you wish to XML-ize your JSPs, the transition will be much easier from s-JSP than from regular JSP.

In my opinion, you better make the decision to use s-JSP before you start writing web applications. In fact, it is always smarter to use EL from the start - conversion to regular JSP is as easy as setting the el-gnored switch to false and scripting-invalid switch to false in web.xml.
Also, you should be very familiar with the Java Standard Template Library (JSTL) and know how to build your own custom actions. Because I could have Java code in JSPs, I neglected these topics only to discover they are important for s-JSP.
Also make sure your IDE supports EL fully. In NetBeans 5.5 (which does fully), I kept on seeing what seems like a discrepancy: when I use a statement like: <c:if test="${empty sessionScope["title"]}"> I get an error. But outside the test, I can obtain the title attribute from the session map by using
${sessionScope["title"]}. I must not be understanding something here. Why do I need to omit quotes (as in <c:if test="${empty sessionScope[title]}">) in the test to make the syntax right? With correct syntax, I can never find my object in the session. It'd seem as if the "title" attribute is a page-scoped object?

As a matter of caution, endeavors like this should be done on test systems first before moving them into production. I'll take this a step further - do changes like this move on test code before integrating them into production code. If I didn't use CVS, I'd be screwed just about now; fortunately, I was able to return to my original code from CVS. Save for the wasted hours, you must plan the move very carefully.