Jun 08

If you require multiple imports then you can either use multiple import page directives, or specify them all in one page directive which each import commas separated

Multiple import directives

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>

One import directive (comma separated)

<%@ page import="java.util.*, java.io.*, java.net.*" %>

written by objects \\ tags: ,

Jun 04

If you are pulling a value from a database ResultSet it is somethimes not possible to check the value returned to determine if the value in the database was null. In these cases you can use the wasNull() method to check.
The wasNull() method will return true if the value in the database was NULL for the last value retrieved from ResultSet.

Here is an example of its usage.

ResultSet rs = statement.executeQuery();
int value = rs.getInt(1);
if (rs.wasNull(1))
{
   // Database contains NULL
}

written by objects \\ tags: , ,

Jun 03

We can use an MD5 implementation of the MessageDigest class to calculate the MD5 sum of a file (or any array of bytes). Once we have that it is simply a matter of feeding the contents of the file to the digest and have it calculate the MD5 sum as shown in the following example.

// Get an MD5 implementation of MessageDigest

MessageDigest digest = MessageDigest.getInstance("MD5");

// Open file and read contents

InputStream is = new FileInputStream(file);				
byte[] buffer = new byte[8192];
int read = 0;
while( (read = is.read(buffer)) >= 0)
{
   // pass data read from file to digest for processing

   digest.update(buffer, 0, read);
}		
is.close();

// Get the MD5 sum

byte[] md5sum = digest.digest();

// (Optionally) convert the MD5 byte array to a hex string

String md5sumHex = new BigInteger(1, md5sum).toString(16);

written by objects \\ tags: , , , ,