04 April 2013

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>

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.

03 April 2013

Developing in Netbeans 7.3

Developing in Netbeans 7.3

Introduction

As an IT consultant, I usually have to work with any PC or laptop the client provides me with. Most of the time, the hardware I work with is not shining new but adequate for developing applications.
However, now and then you come at very big clients, which have thousands of employees and give the same, crappy laptop to all of them. Project managers working on Outlook and Office, testers working on the intranet and testing web applications using different browsers, and us, the developers.
While working on spreadsheets in Excel or clicking around in a web application doesn't require a state-of-the-art PC, developing Java code in an IDE while running a local Weblogic instance and an Oracle XE database DOES...
I was tired of waiting for Eclipse to compile my code, to publish my changes to Weblogic or to restart a database... I just wasn't performing really good and was browsing the web while waiting a lot!

Because of this, as a first improvement, I wanted to change IDEs and try something different. I chose Netbeans as my first test subject!

Fast

The first thing I realized when using this IDE (Netbeans 7.3) was: fast!!!
This IDE is so much better at doing what it has to do, and nothing more, avoiding the constant question: "what's it doing now again?" and "why am I seeing 25 of these "JPA Java Change Event Handler" events in my progress pane?"

Maven

Netbeans uses Maven to manage the projects in the IDE, using it to structure, build, test, run and deploy your applications. It also supports various other build tools (Ant) through the use of plugins. Awesome!

Free

Netbeans is free. Bitchin'.

Debugging

Once you fix the bug where debugging is extremely slow in Netbeans (check this post for 7.1.1 or this for 7.3), it actually becomes so much fun and extremely effective to debug your application!

Built-in support for Application Servers (Glassfish/Weblogic)

I didn't have to do anything fancy to deploy my application to Glassfish or Weblogic. Just right-click the project, click 'Run' and it will start your application server, deploy your application and open your browser to the correct starting page. Easy!
Also, profiling works out-of-the-box in Netbeans, and quite well, actually.

Conclusion

I couldn't be happier developing in Netbeans at the moment. It opens files faster, it runs faster, it compiles faster and it makes my life as a developer much easier!

24 May 2012

Blog Reboot


Just to mention to whoever is reading, that I will start with more regular blog posts again!
The purpose of these blogs is twofold.

restarting...

Firstly, it will act as an archive for myself where I can write down and rant about work, society and life in general.
Secondly, it's to present some of you with my views on current technologies, as well as things that bother or interest me.
So, expect to see a new blog in one of the coming days, yet I still don't know about what :-)