I have the following block of code that works just fine:
<%@page import="java.util.*" %>
<%@page import="java.security.*" %>
<%
String str = "A string to hash.";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update( str.getBytes() );
byte[] digest = md.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0, j = digest.length; i < j; i++) {
    String tmp = Integer.toHexString(0xFF & digest[i]);
    if (tmp.length() < 2) {
        tmp = "0" + tmp;
    }
    hexString.append(tmp);
}
out.println(hexString.toString());
%>When I tried to break the hashing code out into a method I got a "NoSuchAlgorithmException" error when defining the MessageDigest object:
<%@page import="java.util.*" %>
<%@page import="java.security.*" %>
<%
String str = "A string to hash";
String md5string = md5hash(str);
out.println(md5string);
%>
<%!
public String md5hash(String str) {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update( str.getBytes() );
    byte[] digest = md.digest();
    StringBuffer hexString = new StringBuffer();
    for (int i = 0, j = digest.length; i < j; i++) {
        String tmp = Integer.toHexString(0xFF & digest[i]);
        if (tmp.length() < 2) {
            tmp = "0" + tmp;
        }
        hexString.append(tmp);
    }
    return hexString.toString();
}
%>To get the JSP to compile, I had to modify it like so:
<%@page import="java.util.*" %>
<%@page import="java.security.*" %>
<%
String str = "A string to hash";
String md5string = md5hash(str);
out.println(md5string);
%>
<%!
public String md5hash(String str) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {}
    md.update( str.getBytes() );
    byte[] digest = md.digest();
    StringBuffer hexString = new StringBuffer();
    for (int i = 0, j = digest.length; i < j; i++) {
        String tmp = Integer.toHexString(0xFF & digest[i]);
        if (tmp.length() < 2) {
            tmp = "0" + tmp;
        }
        hexString.append(tmp);
    }
    return hexString.toString();
}
%>Why did I have to add a useless try/catch to make this code work?
 
     
     
    