Monday, November 24, 2014

Don't use System.exit() on Java Web Application

I have recently come across a code snippet, where programmer was using System.exit() if application failed to acquire necessary resource after couple of retry. His reasoning was that since, application cannot function, if essential resources like database is not available or there is no disk space to write records in File system. Ok, I hear you; but System.exit() in Java Web application, which runs inside either web server or application server, which itself is Java program is not a good idea at all. Why? because invoking System.exit() kills your JVM, invoking this from Tomcat or Jetty, will not only kill your application but most likely server itself. This can be potentially dangerous, if that server also host other critical application, which is not uncommon at all. As per my experience, System.exit() calls are quite common in overly broad try-catch blocks in web application start-up code that loads environment variables, properties files, connect to MQ Series, establishes database connection, opens socket connections, etc. This is still ok, if you are writing core Java based server, where each application has their own JVM, but with web application deployed on Tomcat, JBoss, WebSphere, Weblogic or any other application server, using System.exit() is big mistake. In worst case can result in outage for lots of other critical application. On the other hand, there are ways to prevent your web application from someone else’s mistake, by enabling Security Manager. System.exit() and Runtime.exit() both goes through the security manager. Enabling Security manager will catch these calls and reduce them into an exception rather than shutting down the whole VM. It's not difficult to enable the security manager in most application servers, Tomcat, JBoss both has documented steps to enable security Manager.

Friday, November 21, 2014

How to loop a HashMap or Hashtable in JSP

Though there are number of ways to loop over HashMap in JSP, or any other Map implementation e.g. Hashtable, I personally prefer JSTL foreach tag for this. As a Java programmer, I often have urge to use Java code directly in JSP using scriptlet, but that's a bad coding practice, and one should always avoid that. Infact by smart use of expression language and JSTL core tag library, you can reduce lot of Java code from JSP. In our last post, we have seen example of JSTL foreach tag to loop over List, but not a Map, and that creates a doubt in one of my readers mind that foreach tag doesn't support Map implementation like HashMap or Hashtable as they are not Collection, but that's not true. JSTL foreach tag has special support for looping over Map, it provides you both key and value by using var attribute. In case of HashMap, object exported using var, contains Map.Entry object. Since Map.Entry has getKey() and getValue() method, you can access them using expression language $(entry.key) and $(entry.value), as we will seen in our example. You can iterate over Map to create table of key and value, or any HTML element e.g. <select>, which needs text and value.</select>

Wednesday, November 19, 2014

Strategy Design Pattern in Java using Enum - Tutorial Example

I have said this before that Java Enum is very versatile and can do lot more than you normally expect from it. We have seen lot of examples of Enum in my earlier posts e.g. writing thread-safe Singleton using Enum and 10 ways to use Enum in Java. In this article, we will  learn a new way to use Enum, for implementing Strategy design pattern. Strategy pattern is one of the famous pattern, which takes advantage of polymorphism, to remove switch cases and strive for open close design principle. Formally it encapsulate related algorithm, known as strategy and make them interchangeable. So your Client, also known as Context, can use different algorithm or strategy, without any modification. One of the key advantage of Strategy pattern is it's extensibility, i.e. introducing new Strategy is as easy as writing a new class and implementing Strategy interface, with Enum, instead of creating separate class, you creates a separate Enum instance, which means less number of classes and full benefit if Strategy pattern.