有帮助的一件事是添加到web-xml
:
<jsp-config>
<taglib>
<taglib-uri>
functions
</taglib-uri>
<taglib-location>
functions.tld
</taglib-location>
</taglib>
</jsp-config>
现在 tomcat (7.0.30) 看到我的 taglib 用于编码 URI。
奇怪的事情:当我用系统打印用户名时,我得到了????在tomcat的控制台而不是象形文字中。也许这指向了问题?在我的控制器中,我有:
final String username = Helpers.decodeRequest(request
.getParameter("user"));
System.out.println("ProfileController.doGet() user name DECODED : "
+ username);
在哪里 :
private static final String CHARSET_FOR_URL_ENCODING = "UTF-8";
public static String decodeRequest(String parameter)
throws UnsupportedEncodingException {
System.out.println(Charset.defaultCharset()); // EDIT: suggested by @Esailija
if (parameter == null)
return null;
System.out.println("decode - request.getBytes(\"iso-8859-1\"):"
+ new String(parameter.getBytes("iso-8859-1")));
System.out.println("decode - request.getBytes(\"iso-8859-1\") BYTES:"
+ parameter.getBytes("iso-8859-1"));
for (byte iterable_element : parameter.getBytes("iso-8859-1")) {
System.out.println(iterable_element);
}
System.out.println("decode - request.getBytes(\"UTF-8\"):"
+ new String(parameter.getBytes(CHARSET_FOR_URL_ENCODING))); // UTF-8
return URLDecoder.decode(new String(parameter.getBytes("iso-8859-1")),
CHARSET_FOR_URL_ENCODING);
}
所以Tomcat:
windows-1252 // EDIT: suggested by @Esailija
decode - request.getBytes("iso-8859-1"):╬╡╬╗╬╗╬╖╬╜╬▒╧?╬▒
decode - request.getBytes("iso-8859-1") BYTES:[B@d171825
-50
-75
-50
-69
-50
-69
-50
-73
-50
-67
-50
-79
-49
-127
-50
-79
decode - request.getBytes("UTF-8"):├Ä┬╡├Ä┬╗├Ä┬╗├Ä┬╖├Ä┬╜├Ä┬▒├?┬?├Ä┬▒
ProfileController.doGet() user name DECODED : ╬╡╬╗╬╗╬╖╬╜╬▒╧?╬▒
???????? // user Dao System.out.println("ελληναρα");
com.mysql.jdbc.JDBC4PreparedStatement@67322bd9: SELECT * FROM users WHERE username='╬╡╬╗╬╗╬╖╬╜╬▒╧?╬▒'
ProfileController.doGet() user : null
日食:
UTF-8 // EDIT: suggested by @Esailija
decode - request.getBytes("iso-8859-1"):ελληναρα
decode - request.getBytes("iso-8859-1") BYTES:[B@44c353ae
-50
-75
-50
-69
-50
-69
-50
-73
-50
-67
-50
-79
-49
-127
-50
-79
decode - request.getBytes("UTF-8"):ελληναÏα
ProfileController.doGet() user name DECODED : ελληναρα
ελληναρα // user Dao System.out.println("ελληναρα");
com.mysql.jdbc.JDBC4PreparedStatement@73aae7c6: SELECT * FROM users WHERE username='ελληναρα'
ProfileController.doGet() user : com.ted.domain.User@4b22015d
编辑:如果我在首选项>工作区>文本文件编码中更改eclipse编码并设置默认值(Cp1252)
windows-1252
decode - request.getBytes("iso-8859-1"):λαλακης
decode - request.getBytes("iso-8859-1") BYTES:[B@5ef1946a
-50
// same bytes ....
decode - request.getBytes("UTF-8"):λαλακη�‚
ProfileController.doGet() user name DECODED : λαλακης
ελληναÏ?α
com.mysql.jdbc.JDBC4PreparedStatement@4646ebd8: SELECT * FROM users WHERE username='λαλακης'
ProfileController.doGet() user : null
并且 Eclipse 也失败了
注意: Tomcat 确实在地址栏中打印了正确的 url
日食很好:
请注意,Firefox 会自动解码 Url(令我困惑)