开发者

Code requires try/catch in JSP declaration section but not in the scriptlet section?

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?


The normal JSP source code is by itself already placed in one huge try-catch block. Every normal scriptlet <% %> will become part of it, so you don't need to explicitly catch the exceptions. However, a method definition <%! %> will be placed outside the standard try-catch, so you have to handle the exceptions yourself.

Needless to say that this is not the best practice. Rather put the Java code in a real Java class. For this particular purpose, I think an EL function is very useful. See also How to avoid Java code in JSP files?


The difference is when you encapsulate the code in a method, checked exceptions thrown within in that method have to be propagated or handled. Exceptions thrown by code in a JSP are getting caught and logged by the servlet container.

In the long term it's best to remove Java code from the JSP. In the short term you can change the declaration of the md5hash method in the second snippet to throw NoSuchAlgorithmException, that way you do without the try-catch and don't eat the exception.

By the way the try-catch is not just useless, it is potentially harmful, since you could have a problem and not get notified about it because of the exception getting eaten.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜