代码需要JSP声明部分中的try / catch,但不需要在scriptlet部分中吗? [英] 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());
%>
当我试图将散列代码分解为方法时,我得到了NoSuchAlgorithmException定义MessageDigest对象时出错:
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();
}
%>
为了让JSP编译,我不得不修改它所以:
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();
}
%>
为什么我要添加一个无用的try / catch来制作这段代码有用吗?
Why did I have to add a useless try/catch to make this code work?
推荐答案
普通的JSP源代码本身已经放在一个巨大的try-catch块中。每个普通的scriptlet < %%>
都将成为其中的一部分,因此您无需显式捕获异常。但是,方法定义<%! %>
将置于标准的try-catch之外,因此您必须自己处理这些例外。
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.
不用说,这不是最好的做法。而是将Java代码放在真正的Java类中。出于这个特殊目的,我认为EL函数非常有用。另请参见如何避免JSP文件中的Java代码?
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?
这篇关于代码需要JSP声明部分中的try / catch,但不需要在scriptlet部分中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!