04/07/2012

[Java] JSP Jasper/Tomcat code too large for try statement error

While working on a rather large JSP with Java and Tomcat, you may have Jasper sooner or later raise the "code too large for try statement" error.

This happens if your JSP contains a lot of code, especially Java code (incapsulated in the <% %> tags) or very very long comments.
In fact, before being presented as an HTML page, the JSP is compiled and in the process, it is enclosed in a try-catch statement which, like any method, has a maximum bytecode size of 64KB (65535 bytes).

The only way to avoid this issue is to split the JSP into multiple pages and then include them using the:

<jsp:include page="file.jsp"/>

directive which compiles the page BEFORE including it instead of:

<% @include file="file.jsp" %>

which embeds the page "as is"

[Java] ConcurrentModificationException

While iterating over any Collection such as Lists, HashSets, etc.. in Java, you may find the need to add or remove items and in doing so you may encounter the ConcurrentModificationException.

You can solve this by cycling over the collection using an Iterator:

Iterator iterator = [collection].iterator();
while (iterator.hasNext()) {
    [type] element = iterator.next();
    if([conditions]){
       iterator.[method];
    }
}

and then calling the iterator.add() or iterator.remove() method.

Note that in later Java versions you may need to type the Iterator as:

Iterator<[type]> iterator = [collection].iterator();

Test the number of different lines between two files with diff and wc

With diff and wc,  you can check the number of differing lines between two files as:

diff file1 file2 | wc -l

[SQL] Left and Right Join inside WHERE condition

In Oracle SQL you can perform a LEFT(or RIGHT) JOIN inside the FROM statement (SQL ANSI-89 standard) with:

SELECT *
FROM table1 RIGHT JOIN table2 ON table1.id1=table2.id2

or inside the WHERE condition as:

SELECT *
FROM table1, table2
WHERE table1.id(+)=table2.id



The LEFT JOIN is achieved with = table2.id(+) instead

Internet Explorer CheckBox onChange event

When working with Internet Explorer, you may notice that the onChange event, when tied to a CheckBox, won't work as you'd expect it to. You may indeed notice that the field linked to the event must lose focus before firing the event.

You can correct this by using the onClick event.

[SQL] String to Date

In SQL, you can create a Date value from a String representation with:

TO_DATE('string_date', 'pattern');

Where string_date is the String representation of the date you are working on, like '31/12/1900' and pattern defines how your date is represented, as 'dd/mm/yyyy' in our example.

You can also test two Date values using  the < = > ! operators.

[Java] Calendar Date to Timestamp

In Java you can convert a Date to a Timestamp simply by:

Date date = Calendar.getInstance().getTime();

This will return the current date, then:

Timestamp timestamp = new Timestamp(date.getTime());

where date.getTime() will return a long.

[Java] Format date with locale

In Java, you may need to format a date using a particular pattern or locale. You can easily do it with SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
Date date = Calendar.getInstance().getTime();
sdf.format(date); 

This code formats the current date using the given pattern and locale.

[SQL] Select column, max(count(column))

In SQL, to know which row id has the MAX(COUNT(*)) in a table, you would like to:

SELECT id, MAX(COUNT(*))
FROM table
WHERE [conditions]

But this will not work on its own, instead you should: