Mark
asked on
noSuchAlgorithmException in jsp subroutine. Why?
I am trying to make a routine that encrypts. I found some code that works. When I put the following in-line in my jsp page, it works fine:
String pw = "mypassword";
java.security.MessageDiges t d =null;
d = java.security.MessageDiges t.getInsta nce("SHA-1 ");
d.reset();
d.update(pw.getBytes());
However, I want to make it a function I can call. I've done this before by including code, but this time I'm having trouble. In my main jsp page I have:
<%@ include file="include/crypt.inc"%>
and in include/crypt.inc I have:
<%!
private byte[] pwCrypt(String pw)
{
java.security.MessageDiges t d =null;
d = java.security.MessageDiges t.getInsta nce("SHA-1 ");
d.reset();
d.update(pw.getBytes());
return d.digest();
}
%>
However, when I attempt to load the jsp page I get the following error, even if I don't try to call pwCrypt():
org.apache.jasper.JasperEx ception: Unable to compile class for JSP:
An error occurred at line: 5 in the jsp file: /include/crypt.inc
Unhandled exception type NoSuchAlgorithmException
2: private byte[] pwCrypt(String pw)
3: {
4: java.security.MessageDiges t d =null;
5: d = java.security.MessageDiges t.getInsta nce("SHA-1 ");
6: d.reset();
7: d.update(pw.getBytes());
8: return d.digest();
What am I doing wrong?
String pw = "mypassword";
java.security.MessageDiges
d = java.security.MessageDiges
d.reset();
d.update(pw.getBytes());
However, I want to make it a function I can call. I've done this before by including code, but this time I'm having trouble. In my main jsp page I have:
<%@ include file="include/crypt.inc"%>
and in include/crypt.inc I have:
<%!
private byte[] pwCrypt(String pw)
{
java.security.MessageDiges
d = java.security.MessageDiges
d.reset();
d.update(pw.getBytes());
return d.digest();
}
%>
However, when I attempt to load the jsp page I get the following error, even if I don't try to call pwCrypt():
org.apache.jasper.JasperEx
An error occurred at line: 5 in the jsp file: /include/crypt.inc
Unhandled exception type NoSuchAlgorithmException
2: private byte[] pwCrypt(String pw)
3: {
4: java.security.MessageDiges
5: d = java.security.MessageDiges
6: d.reset();
7: d.update(pw.getBytes());
8: return d.digest();
What am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
never mind. I moved the 'return' outside the try/catch block and it works now. Thanks. Here is the final code:
<%!
private byte[] pwCrypt(String pw)
{
java.security.MessageDiges t d =null;
try {
d = java.security.MessageDiges t.getInsta nce("SHA-1 ");
d.reset();
d.update(pw.getBytes());
}
catch (Exception e){
e.printStackTrace();
}
return d.digest();
}
%>
<%!
private byte[] pwCrypt(String pw)
{
java.security.MessageDiges
try {
d = java.security.MessageDiges
d.reset();
d.update(pw.getBytes());
}
catch (Exception e){
e.printStackTrace();
}
return d.digest();
}
%>
ASKER
return d.digest();
so what's up now?
org.apache.jasper.JasperEx
An error occurred at line: 2 in the jsp file: /include/crypt.inc
This method must return a result of type byte[]
1: <%!
2: private byte[] pwCrypt(String pw)
3: {
4: try {
5: java.security.MessageDiges