Solved

noSuchAlgorithmException in jsp subroutine. Why?

Posted on 2007-11-19
3
1,188 Views
Last Modified: 2012-06-22
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.MessageDigest d =null;
     d = java.security.MessageDigest.getInstance("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.MessageDigest d =null;
     d = java.security.MessageDigest.getInstance("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.JasperException: 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.MessageDigest d =null;
5:      d = java.security.MessageDigest.getInstance("SHA-1");
6:      d.reset();
7:      d.update(pw.getBytes());
8:      return  d.digest();

What am I doing wrong?
0
Comment
Question by:jmarkfoley
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 20317143
You have to put a try/catch block to catch the NoSuchAlgorithmException, or you have to throw it from your subroutine.

See  the API doc here:
http://java.sun.com/javase/6/docs/api/index.html?java/security/package-tree.html
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20317310
Getting closer. Now it is telling me that I must return a result of type byte[], but the class def. for digest() says it is type byte[]. In my code (see above) I do:

return  d.digest();

so what's up now?


org.apache.jasper.JasperException: Unable to compile class for JSP:

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.MessageDigest d =null;

0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20317336
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.MessageDigest d =null;

    try {
        d = java.security.MessageDigest.getInstance("SHA-1");
        d.reset();
        d.update(pw.getBytes());
    }
    catch (Exception e){
      e.printStackTrace();
    }

    return  d.digest();
}
%>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best tutorial on log4j and logging for servlets/jsp codes 1 65
caughtSpeeding java challenge 16 139
'exit' is not valid keyword in java 14 160
oracle fusion vs MQ 2 122
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question