悬疑灵异

服务器下页面中文问题解覆盖

目前, Tomcat 作为一种出色的开放源代码的 JSP 服务器,目前在 JSP 的开发过程中获得了广泛的应用. 但是作为一款英语国家公司开发的软件, 在中文环境下不可避免的会出现一些乱码问题. 这里就 Tomcat 4.0 和 Tomcat 4.1 下的常见中文问题及其解决方法做一个总结. 这些方法都已经在 中文版 Windows 98 + JDK 1.3.1 和 中文版 Windows 2000 + JDK 1.3.1 下通过了测试. 另外在 IBM 的站上有一个页 也讨论了这个问题. 首先为了便于讨论, 这里首先列出了一些方便的工具方法, 便于我们的讨论. 这些方法如下所示: // 转换由表单读取的数据的内码到 GB2312  public String toChi(String input) {  try {  byte[] bytes = tBytes(\"ISO\");  return new String(bytes);  }catch(Exception ex) {  }  return null;  }    // 对给定字符进行 URL 编码  public String encode(String value) {  if(isEmpty(value)) return \"\";  return code(value);  }    // 对给定字符进行 URL 解码  public String decode(String value) {  if(isEmpty(value)) return \"\";  return code(value);  }

问题1. 浏览器中看到的 JSP 页面中的汉字怎么都成了 \'?\' ? 可能原因如下: 您的页面中没有指定页面的字符集为中文. 解决方法(适用于Tomcat 4.0 和 Tomcat 4.1)是在页面中添加如下代码: %@ page contentType=\"text/html;charset=gb2312\" %  meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"

问题2. 通过 POST 方法提交的表单的汉字都显示为乱码(在 Tomcat 4.0 下正常, Tomcat 4.1 下出现). 可能原因如下: POST 提交的字符串都是 ISO 编码的, 只要把它的字符集转换到中文就行了. 解决方法如下(适用于 Tomcat 4.1): // 单个的参数  String result = toChi(tParameter(\"parameterName\"));  // 多个参数  String values[] = tParameterValues(name);  if(values != null) {  for(int i = 0; i ngth; i++) {  values[i] = toChi(values[i]);  }  }

后金的军队已经打到了山海关 问题3. 通过 GET 方法提交的表单的汉字都显示为乱码(在 Tomcat 4.0 和 Tomcat 4.1 下都出现).可能原因如下: GET 提交的字符串都是 ISO 编码的, 只要把它的字符集转换到中文就行了. 解决方法如下(适用于 Tomcat 4.1, Tomcat 4.0 下不能用于 p?username=中文): // 单个的参数  String result = toChi(tParameter(\"parameterName\"));  // 多个参数  String values[] = tParameterValues(name);  if(values != null) {  for(int i = 0; i ngth; i++) {  values[i] = toChi(values[i]);  }  }

问题4. Cookie 中不能写入汉字或者汉字无法正确显示. 可能原因如下: Tomcat 4.0 下自动把 Cookie 做了编码为 ISO 的存储, 而 Tomcat 4.1 下的 JSP 引擎不支持包含含有汉字的 Cookie. Tomcat 4.0 下的解决方法: // 根据 Cookie 名称得到请求中的 Cookie 值, 如果 Cookie 值是 null, 则返回 \"\"  public String getCookieValue(HttpServletRequest request, String name) {  Cookie[] cookies = tCookies();  if(cookies == null) return \"\";  for(int i = 0; i ngth; i++) {  Cookie cookie = cookies[i];  if(tName().equals(name)) {  // 需要对 Cookie 中的汉字进行 URL 反编码, 适用版本: Tomcat 4.0  return decode(tValue());  }  }  // A cookie might not return a null value, may return a \"\"  return \"\";  }

Tomcat 4.1 下的解决方法: // 写入包含汉字 Cookie 的方法  dCookie(new Cookie(\"cookieName\", encode(\"汉字\")));  // 得到 Cookie 值的方法(同 Tomcat 4.0 的解决方法)  public String getCookieValue(HttpServletRequest request, String name) {  Cookie[] cookies = tCookies();  if(cookies == null) return \"\";  for(int i = 0; i ngth; i++) {  Cookie cookie = cookies[i];  if(tName().equals(name)) {  // 需要对 Cookie 中的汉字进行 URL 反编码, 适用版本: Tomcat 4.0  return decode(tValue());  }  }  // A cookie might not return a null value, may return a \"\"  return \"\";  }

问题5. 在 Tomcat 4.0 下 GET 请求(如: p?username=中文) 无法返回原来的值. 原因: 与 Tomcat 引擎有关, 不论是否转换内码, 均无法返回原来的值, 但是有一个替代方法, 如下: 将 URL 地址改变为 \"p?username=\" + encode(\"中文\") 然后使用下列代码取回参数: // 单个的参数  String result = toChi(tParameter(\"parameterName\"));

查看本文来源

杭州宫颈糜烂哪家好
哈尔滨白癜风治疗医院
广东新标木门怎么样
友情链接