Migrating from Tomcat to Weblogic 10.3.3
Chapter 1: Compiling JSPs
In many large, old web applications, you will come across JSPs which are filled with scriptlets and Java code, as it was the common practice "back in the day" to intermix the logical layer with the presentation layer.
Nowadays, this is very much frowned upon, because when using the latest web frameworks such as JSF or Spring MVC (as a few of the examples), is has become much easier to work using one of the "separation of concerns" programming patterns such as the Model-View-Controller pattern.
The most common problem when migrating from Tomcat to Weblogic is that a lot of the JSPs just won't compile anymore. This is due to the higher strictness of the Weblogic JSP compiler than the regular Tomcat compiler.
In this blog post, I will describe a couple of problems (and solutions!) you might encounter when trying to get these JSPs to compile again.
Quotes within Quotes
This is a very common mistake in old JSPs, usually because of overuse of scriptlets inside a JSP. I'll demonstrate this using an example:
<a href="javascript:gotoPage('<%= bean.getProperty("page") %>')">LINK</a>
<a href="javascript:gotoPage('<%= bean.getProperty("page") %>')">LINK</a>
You might already notice there's a lot of bad practices in the line above, but in a regular servlet container such as Tomcat, this will compile and run fine.
On an application server though, this won't compile and if you have many hundreds of JSPs like this, will cost you lots of time and refactoring work.
First, let's see what is wrong with that line of code:
- Using double quotes within double quotes - not allowed according to specs even though Tomcat forgives you
- Using scriptlets inside HTML tags - not recommended, bad practice
- Using JavaScript methods inside the href attribute - using the onclick method is better
Secondly, how can we fix this?
The easiest way to make this piece of code compile is the following:
<a href="javascript:gotoPage('<%= bean.getProperty(\"page\") %>')">LINK</a>
But I don't like this solution, as it's still very messy and prone to bugs. It's probably better to try and leave out the JavaScript and put that in the bean class:
<a href="<%= bean.getJSLinkForPage('page') %>">LINK</a>
This won't compile though, since the single quoted value 'page' is incorrect Java code, since it is interpreted as a char value. You can solve this by reversing the quotes:
<a href='<%= bean.getJSLinkForPage("page") %>'>LINK</a>
Still, this can be improved by using JSTL instead of scriptlets, which is much more clean and forces the developer to think about how he can write better code:
<a href="${bean.jsLink}">LINK</a>
The line of code above is about as simple and clean as u can get, forcing the developer to write the controller code in Java, resulting in less bugs. You can spot compiler errors straight away and you can unit test the code, which is a big plus.