Thursday, June 21, 2007

Javascript URL encoding

If you are using AJAX or something else to communicate via url, you can use the javascript function "escape()" to encode parts of the URL. I list some of the characters that are encoded and some that are not:

Encoded: ! # $ % ^ ( ) = [ ] { } ; " \ ' ? / ,
Not encoded: underscore @ & * - + .

You can also use the javascript function "encodeURI". It has a different set of encoded characters:

Encoded: ` ^ [ ] { } " < > \
Not encoded: underscore tilde ! @ # & $ * ( ) = + , . : ; ' / ?

Wednesday, May 9, 2007

Doctype and browser compatibility

I recommend setting a doctype in your web pages. If you specify the right doctype, then you can avoid IE dropping into "quirks mode" ("quirks mode" = IE backwords compatibility mode). Thus, your pages will look almost the same in IE and Firefox.

You can get a list of doctypes here:
http://www.w3.org/QA/2002/04/valid-dtd-list.html

If you specify an "HTML" doctype, and use DHTML, then your jsp pages may still be in quirks mode. I recommend using an XHTML doctype to solve this problem, like this doctype for example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

Wednesday, May 2, 2007

SQL: Adding foreign key relationships

Tip on adding foreign key relationships in SQL:

You can establish a foreign key relation in your create table or with an alter table statment, like this:

ALTER TABLE [table1_name]
ADD CONSTRAINT [constraint_name]
FOREIGN KEY ([field(s) from table1])
REFERENCES [table2_name] ([field(s) from table2])
ON [DELETE CASCADE, or UPDATE, or other command];

(BTW, in the example above, for a cascade, generally table1 would be the child table, and table2 would be the parent table)

If you get an error like this:
Error Code : 1005
Can't create table '.\test\#sql-ac_195.frm' (errno: 150)

it may be the case that your referenced field is not guaranteed unique, which it must be. If you don't have a single unique field in that table, you can use multiple fields to specify the unique key.

Tuesday, May 1, 2007

Establishing primary key auto creates index

In MySQL, you can have a primary key constraint on one or more columns. Establishing a primary key causes MySQL to automatically make a unique key that consists of the columns specified in the primary key. You can verify the existence of this key by running the command:

SHOW INDEX FROM [table name];

In the resulting output, the column 'Non_unique' shows '0' if the column is unique, and '1' if the column is not unique.

SQLyog

I want to recommend the software I use for managing my MySQL databases. It is called SQLyog. You can find it online for download. It is free, although you can buy upgraded versions. It is great for running queries, manipulating data, imports, exports, and you can even manage indexes/triggers/etc.

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.