Wednesday, April 18, 2007

Javascript and Java parseInt

Make sure to specify the radix (base) when using parseInt to convert from string to numerical values. Otherwise if your number is prefixed with zero, a non decimal base is assumed!

In Java:
n = Integer.parseInt(s,10);

In Javascript:
n = parseInt(s,10);

From w3Scholls.com Javascript reference website:

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)
So, you can see how important it is to specify the radix in the case where you are parsing date strings like 08-08-2007. Without specifying the radix, you'd end up with Oct 10, 2007!

Java string comparison tip

Today's tip is just a simple reminder to always use th "equals" function when comparting Strings in Java and not the comparison operator "==".

For example:

String a="a";

//Good
if (a.equals("a"))
fnA();

// Bad
if (a=="a") fnA();

The comparsion operator will compare the value of the String pointer which is probably not what is desired. Generally, you should use the equals function to compare the contents of teh string.

Tuesday, April 17, 2007

MySQL pagination

I'm writing some pagination code for my application. Turns out MySQL has a great method for accomplishing this.

In the select statement, just end it with
LIMIT [offset,] number_of_rows

Combine this with distinct ordering via "ORDER BY" on a unique indexed key, and you have the database doing the pagination without even having to keep open a connection.

Exceeding the 65535 bytes limit

Quite a while ago, I was working on my server-side Java & JSP application, when I encountered this error:

org.apache.jasper.JasperException: Unable to
compile class for JSP
Generated servlet error:

The code of method
_jspx_meth_html_html_0(javax.servlet.jsp.PageContext) is exceeding the 65535
bytes limit



Last time I hit a code size limit was 14 years ago when I was compiling computer programs in Borland Turbo C. Back then, I found the solution was to split the source files.

In this case, I was using the Struts framework, Eclipse/MyEclipse and trying to load the app with Apache Tomcat 5.5.9. What was happening is all the jsp files are being server-side compiled into too large a file. It won't help then to split the files into more jsp files, as these are all compiled in on the server side.

To solve the problem, I broke out all the javascript functions in the jsps into Javascript (.js) files. These cannot access server data, but they don't count towards the size limit as they are not compiled into the server. This worked; I haven't had any more such problems.

Be sure to put anything that does not have a jsp or struts tag into a JS file and not a JSP file. This will help your application performance and avoid file size caps.

You can also put the jsp content in an html file and use an html include:
<!--#include virtual="my_file.html" -->
or
<!--#include file="my_file.html" -->

Monday, April 16, 2007

HTML HREF and onclick javascript functions

If you have an HTML HREF element just to use the onclick handler to launch a javascript function, make sure to add a "return false;" at the end of the onclick handler to prevent unwanted navigation. If the "return false;" is not specified, then after the handler is executed, the default action will be run, which is to navigate to the location specified in the "href=".

For example:

<a href="#" onclick="myfunction(); return false;">click me </a>

Alternatively, you could write the above line without the onclick mentioned. Then just put an event handler in your Javascript code.

AJAX in IE - thread safe

If you write an AJAX function, be sure to use Microsoft.FreeThreadedXMLDOM rather than Microsoft.XMLDOM if you want to ensure only 1 thread can access your xml objects at a time.

For example, you may want to have this:

x = new ActiveXObject("Microsoft.FreeThreadedXMLDOM");

rather than this:

x = new ActiveXObject("Microsoft.XMLDOM");

-Dan Burke

Welcome

Welcome to my blog. This is where I'll post useful tidbits of web development information that I believe will be helpful to other programmers.

-Dan Burke