Monday, April 30, 2007

Magic of JQuery

We know that in IE, a form gets submitted whenever we press 'Enter' in a text-field. To disable it would require trapping the event in Javascript and disabling the default behaviour.

<input onkeydown="if (event.keyCode == 13){returnfalse;}" name="email">

But to do this in all text fields across hundreds of html pages would be a impossible task. Then suddenly I got the idea of using JQuery's powerful selector concept to apply this to all textfields across all pages. I just needed to add the following lines to the JS file that was present in all pages.

<script src="jquery-1.1.2.js"></script>
<script type="text/javascript">
$("input[@type=text]").keypress( function() {
if (event.keyCode == 13){return false;}
} );

Friday, April 27, 2007

Mean, Median, Mode, Midrange and Standard Deviation

A definition of terms to help my frail memory when I need it most :)

The arithmetic mean is the sum of the numbers, divided by the quantity of the numbers.

The geometric mean of two numbers is the square root of their product.The geometric mean of three numbers is the cubic root of their product.

The quantity obtained by adding the largest and smallest values and dividing by 2, statisticians call the midrange.

The median is the central point of a data set. To find the median, you would list all data points in ascending order and simply pick the entry in the middle of that list. If there are an even number of observations, the median is not unique, so one often takes the mean of the two middle values.

The standard deviation is the most common measure of statistical dispersion, measuring how widely spread the values in a data set are. If the data points are close to the mean, then the standard deviation is small. Conversely, if many data points are far from the mean, then the standard deviation is large. If all the data values are equal, then the standard deviation is zero.It is defined as the square root of the variance.

For lists, the mode is the most common (frequent) value. A list can have more than one mode. For histograms, a mode is a relative maximum ("bump"). A data set has no mode when all the numbers appear in the data with the same frequency. A data set has multiple modes when two or more values appear with the same frequency.A distribution with two modes is called bimodal. A distribution with three modes is called trimodal.

Wednesday, April 25, 2007

DST implementations in Operating Systems and Java

I often wondered how operating systems knew about Day light saving offsets. Recently USA decided to change its DST settings and it immediately crossed my mind as to how Operating Systems and Applications would react to this.

Some basics first - In what format does a OS store date/time information ? All machines keep their clocks in synch with the GMT using Network Time Protocol. Then the machine calculates it local time using the local time zone settings that are stored on the machine.
Local time is always computed as = GMT + timezone offset + DST offset

But where does the OS get information about DST. All DST information is stored in a binary database called ZoneInfo database or TimeZone DB. On Solaris this database can be found at "/usr/share/lib/zoneinfo"

In Java, the JRE picks up DST information from the Timezone database stored at "%JRE_HOME%/lib/zi"

Tip: In Java, U can change the default timezone that the application uses by setting the user.timezone property
e.g. java -Duser.timezone=Area/City
java -Duser.timezone=GMT[+5.30]

web.xml SAXParseException in Websphere v6.1

I encountered a wierd problem while re-deploying a war file on Websphere v6.1. I started getting a SAXParseException in web.xml. The only change I had made was to introduce the "error-page" tag.

The war file was working fine in Tomcat, Weblogic and Geronimo. Digging into the logs of Websphere, I found this message:

org.xml.sax.SAXParseException: The content of element type "web-app"
must match "(icon?,display-name?,description?,distributable?,
login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)". [wsjspc]

I wondered if the order of the tags was important to WAS. So I placed the error-page tag between the welcome-file-list and taglib tags..and the SAXException disappeared :)
But I found it rather strange that the order of the tags should be important..not sure if it is part of the servlet spec.

Tuesday, April 24, 2007

Cool tools - Squirrel SQL Client, IzPack Java Installer

Came across 2 tools that I liked :)

Squireel SQL client: Use it to connect to any database using JDBC.

IzPack Java Installer: A simple opensource free installer for Java. But this requires that JDK be present on the installation machine.

Show friendly HTTP error messages in IE

In web.xml, I added the tag to redirect to an error page when ever a 500 internal server error occurs. But in IE, the page was still not getting displayed.

I added a sniffer to check what was going on. I could see that the custom error page was returned by the server in the HTTP response, yet IE was ignoring it. Googling around, I found out that IE has a feature turned on by default - Show friendly HTTP error messages (goto tools-> internet options-> advanced -> uncheck show friendly HTTP error message)

If I unchecked this checkbox, then the custom page was getting displayed correctly. But this was obviously not a solution that was practical. I perused thru more info about this IE feature here. IE uses this feature only if the response content for a 500 status-code response was less than 512 bytes.

So now, I suddenly had 2 solutions to the above problem (without having to change IE settings on the client)
Solution 1: Increase the content of the error page to more than 512 bytes.
Solution 2: Change the HTTP status code sent in the response header of the error page to HTTP 200.
Code snippet (at the top of the error jsp page):

Monday, April 23, 2007

UnsatisfiedLinkError while loading native libraries

I encountered the following problem when calling native code from a Java application deployed on Websphere.
Whenever I was redeploying the WAR file, I started getting UnsatisfiedLinkError - native library already loaded by a classloader.

I knew that the call to "System.loadLibrary" should occur only once in a classloader context. We typically put this code in a static block of a class. But it seemed that in Websphere, when the web application context (classloader) was reloaded, the native libraries were not unloaded. Hence whenever I reloaded the webapp, the above error manifested itself.

To solve the above problem, there were 2 options:
- Put the call to System.loadLibrary inside a static block of a class and load this class as a shared library in Websphere. This will load the class using the System classloader and not the web-application classloader.
- Restart the application server everytime the war/ear file was updated. This solution may or may not be feasible depending on your infrastructure setup.

Imp Caveat: Any crash in the call to the native library crashes the JVM itself. Hence for critical applications, other design alternatives should be thought of. For e.g. writing a separate application that will have a RMI/SOAP/XML interface that will do all the JNI plumbing. This separate application would run in a separate JVM outside of the JEE container JVM. Your webapp would then call this application.

Friday, April 20, 2007

Sequence numbers from a Oracle RAC cluster

Today, I faced a peculiar problem. Our application was using a Sequence to autogenerate unique numbers required for a particular functionality. When testing the application against a standalone Oracle server, the sequence numbers generated were in order.

But in the production evnironment, where we were using a Oracle RAC cluster, the sequence numbers returned were not in order..This was creating a lot of confusion. The reason was that by default sequences in 10g RAC are created without ordering. This is to reduce performance bottleneck as there would be synchronization requried across all nodes of the cluster. Each node creates a cache of the sequence as specified in the SQL.

If we need to maintain the order then we need to add the ORDER argument to the sequence.
Example of Sequence Syntax in Oracle RAC:


Thursday, April 12, 2007

NTLM authentication using Commons HttpClient

Recently I wanted to access a site outside the firewall thru Apache commons HttpClient. The proxy server was using NTLM authentication.

Thankfully, HttpClient had support for NTLM authentication. Here's the code snippet for the same:
HostConfiguration hConf= httpClient.getHostConfiguration();
hConf.setProxy("", 8080);

new AuthScope("", 8080, "domainName"),

new NTCredentials("username", "password","my_localhost","domainName")


Reset method of FormBeans in Struts

We all know that FormBeans in Struts can have either a 'request' scope or a 'session' scope.

In earlier versions of Struts, I recollect that the reset method of the FormBean was called by the Struts framework whenever the scope is request and not called for session scope. This was because, for 'Request' scope, a pool of FormBean objects were created.

But with new versions of JDK (especially since JDK1.4) object creation is no longer a performance penalty. In fact, short-lived objects are quickly garbage collected because they are allocated in the younger generation of the heap.

Looking at the latest Struts source code (v1.9), I saw that if the scope of the FormBean is Request, then a new FormBean object is created for each request.

Also the reset method is called for both request and session scope FormBeans.
But there is an important difference - if the scope is request, then the constructor of the formbean is also called everytime (for each request), where as for session scope the constructor would be called only once.

So, now what to do? The default reset method does nothing. We only need to override it in the following cases:

1. There are checkboxes and form-bean scope is session: In the reset method we need to reset the checkbox fields, because the browser does not submit a checkbox value if it is clear.

2. Nested beans, arrayLists are used and scope is request: In this case, we need to create a arrayList with the correct size.

Wednesday, April 11, 2007

JQuery Javascript library

Came across this cool cool Javascript library that adds that extra jazz to Javascript programming.

Some concepts I liked in this library:
- Separation of presentation markup with behavior (event handling etc.)
- Cool implementation of Chain of Responsibility pattern.
- Good effects such as slow show etc.

Overall, a good library that can be used in web projects.