【问题标题】:apache tomcat and glassfish server not decoding utf-8 characters?apache tomcat 和 glassfish 服务器不解码 utf-8 字符?
【发布时间】:2023-03-28 12:20:01
【问题描述】:

这是我的 Jsp 页面:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
 <body>
<%
String msg="206_John_help i m in trouble,delhi,อินเดีย_30.64741430_76.817313799";
String result = java.net.URLEncoder.encode(msg, "UTF-8");
System.out.println("The msg is "+result);
String result1=java.net.URLDecoder.decode(result, "UTF-8");
System.out.println("The decoded msg is "+result1);

%>
</body>
</html>

输出是 206_John_help i m in trouble,delhi,???????_30.64741430_76.817313799

我总是得到 ??????而不是泰文字母。解码时如何获取泰语字母?

【问题讨论】:

  • 尝试编写包含泰文字符的hello.html,将其放在文件系统和tomcat下,然后尝试用浏览器打开它。在这种情况下你能正确看到泰语字符吗?

标签: java jsp tomcat utf-8 glassfish


【解决方案1】:

您可以尝试在顶部添加:

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

我认为这将解决问题。如果没有,您能否提供更多信息?

你在使用eclipse吗?如果是,你是在windows下开发吗? 在 windows 中,eclipse 默认使用 WINDOWS CP1252 对 jsp 和其他文件进行编码。大概写的文字可以是CP1252 charset。

tomcat 和 glashfish 在哪里运行?视窗? Linux?

【讨论】:

  • 我运行 glassfish 作为本地服务器,但运行 apache tomcat 作为在线服务器。不,我没有使用 eclipse,我使用的是 netbeans。上述解决方案不起作用。我使用的是 windows
【解决方案2】:
 Try to call these 2 methods before encoding and decoding.  
 String msg="206_John_help i m in trouble,delhi,อินเดีย_30.64741430_76.817313799";
 String result = java.net.URLEncoder.encode(convertFromUTF8(msg), "UTF-8");
 System.out.println("The msg is "+result);
 String result1=java.net.URLDecoder.decode(convertToUTF8(result), "UTF-8");
 System.out.println("The decoded msg is "+result1);     
 /* convert from UTF-8 encoded HTML-Pages -> internal Java String Format */
 public static String convertFromUTF8(String s) {
 String out = null;
 try {
 out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
 } catch (java.io.UnsupportedEncodingException e) {
 return null;
 }
 return out;
 }

 /* convert from internal Java String Format -> UTF-8 encoded HTML/JSP-Pages  */
 public static String convertToUTF8(String s) {
 String out = null;
 try {
 out = new String(s.getBytes("UTF-8"));
 } catch (java.io.UnsupportedEncodingException e) {
 return null;
 }
 return out;
 }

【讨论】:

  • 在 并在你的 jsp 中的任何地方调用它
  • 它正在将 1 个源文件编译到 C:\Users\Java\Documents\NetBeansProjects\demos\build\generated\classes C:\Users\Java\Documents\NetBeansProjects\demos\build\生成\src\org\apache\jsp\new_0020jsp1_jsp.java:57:错误:找不到符号NewClass1 nw=new NewClass1(); ^ 符号:类 NewClass1 位置:类 new_0020jsp1_jsp C:\Users\Java\Documents\NetBeansProjects\demos\build\generated\src\org\apache\jsp\new_0020jsp1_jsp.java:57:错误:找不到符号 NewClass1 nw=new NewClass1 (); ^ 符号:类 NewClass1
  • 你从哪里得到NewClass。你要么传递完整的代码,要么告诉细节。我给你的解决方案是有效的,只有在你没有将服务器配置为 UTF-8 时才需要它。要么去服务器。 xml 并更改连接器以接受 UTF-8。
【解决方案3】:

你能检查一下 netbeans 中的 jsp 字符集编码吗?我认为这是问题所在。 可能JSP是用windows charset编译的,也就是说msg字符串值会被编译到windows charset中,那么用jsp写的文本就会被编码到这个charset中。

尝试配置netbeans以utf-8字符集编译jsp而不是windows cp1252,然后重试。

【讨论】:

猜你喜欢
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 2015-05-09
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多