服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

tomcat服务器下jsp页中文问题解决方法


  目前, tomcat 作为一种出色的开放源代码的 jsp 服务器,目前在 jsp 的开发过程中获得了广泛的应用. 但是作为一款英语国家公司开发的软件, 在中文环境下不可避免的会出现一些乱码问题. 这里就 tomcat 4.0 和 tomcat 4.1 下的常见中文问题及其解决方法做一个总结. 这些方法都已经在 中文版 windows 98 + jdk 1.3.1 和 中文版 windows 2000 + jdk 1.3.1 下通过了测试.
  
  首先为了便于讨论, 这里首先列出了一些方便的工具方法, 便于我们的讨论. 这些方法如下所示:
  
  // 转换由表单读取的数据的内码到 gb2312
  public string tochi(string input) {
  try {
  byte[] bytes = input.getbytes("iso8859-1");
  return new string(bytes);
  }catch(exception ex) {
  }
  return null;
  }
  
  // 对给定字符进行 url 编码
  public string encode(string value) {
  if(isempty(value)) return "";
  return java.net.urlencoder.encode(value);
  }
  
  // 对给定字符进行 url 解码
  public string decode(string value) {
  if(isempty(value)) return "";
  return java.net.urldecoder.decode(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 提交的字符串都是 iso8859-1 编码的, 只要把它的字符集转换到中文就行了. 解决方法如下(适用于 tomcat 4.1):
  // 单个的参数
  string result = tochi(request.getparameter("parametername"));
  // 多个参数
  string values[] = request.getparametervalues(name);
  if(values != null) {
  for(int i = 0; i < values.length; i++) {
  values[i] = tochi(values[i]);
  }
  }
  
  问题3. 通过 get 方法提交的表单的汉字都显示为乱码(在 tomcat 4.0 和 tomcat 4.1 下都出现).
  可能原因如下: get 提交的字符串都是 iso8859-1 编码的, 只要把它的字符集转换到中文就行了. 解决方法如下(适用于 tomcat 4.1, tomcat 4.0 下不能用于 page.jsp?username=中文):
  // 单个的参数
  string result = tochi(request.getparameter("parametername"));
  // 多个参数
  string values[] = request.getparametervalues(name);
  if(values != null) {
  for(int i = 0; i < values.length; i++) {
  values[i] = tochi(values[i]);
  }
  }
  
  问题4. cookie 中不能写入汉字或者汉字无法正确显示.
  可能原因如下: tomcat 4.0 下自动把 cookie 做了编码为 iso8859-1 的存储, 而 tomcat 4.1 下的 jsp 引擎不支持包含含有汉字的 cookie.
  tomcat 4.0 下的解决方法:
  // 根据 cookie 名称得到请求中的 cookie 值, 如果 cookie 值是 null, 则返回 ""
  public string getcookievalue(httpservletrequest request, string name) {
  cookie[] cookies = request.getcookies();
  if(cookies == null) return "";
  for(int i = 0; i < cookies.length; i++) {
  cookie cookie = cookies[i];
  if(cookie.getname().equals(name)) {
  // 需要对 cookie 中的汉字进行 url 反编码, 适用版本: tomcat 4.0
  return decode(cookie.getvalue());
  }
  }
  // a cookie might not return a null value, may return a ""
  return "";
  }
  tomcat 4.1 下的解决方法:
  // 写入包含汉字 cookie 的方法
  response.addcookie(new cookie("cookiename", encode("汉字")));
  // 得到 cookie 值的方法(同 tomcat 4.0 的解决方法)
  public string getcookievalue(httpservletrequest request, string name) {
  cookie[] cookies = request.getcookies();
  if(cookies == null) return "";
  for(int i = 0; i < cookies.length; i++) {
  cookie cookie = cookies[i];
  if(cookie.getname().equals(name)) {
  // 需要对 cookie 中的汉字进行 url 反编码, 适用版本: tomcat 4.0
  return decode(cookie.getvalue());
  }
  }
  // a cookie might not return a null value, may return a ""
  return "";
  }
  
  问题5. 在 tomcat 4.0 下 get 请求(如: page.jsp?username=中文) 无法返回原来的值.
  原因: 与 tomcat 引擎有关, 不论是否转换内码, 均无法返回原来的值, 但是有一个替代方法, 如下:
  将 url 地址改变为 "page.jsp?username=" + encode("中文")然后使用下列代码取回参数:
  // 单个的参数
  string result = tochi(request.getparameter("parametername"));
  
  问题6. javabean 里使用 <jsp:setproperty id="propname" property="*" /> 出现中文问题暂时未提供解决方案.

扫描关注微信公众号