服务热线:13616026886

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

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

java多语言编码问题解析(2)


  这里是一个具体的例子:
  
  现在因为浏览器对utf-8的支持,我们可以通过在源文件、请求、响应中都使用unicode编码方式,来轻松达到处理国际化和字符编码问题的目标。
  以我们使用的tomcat4.1.2为例,过程如下:
  
  1、编写jsp页面时:在每个jsp页面在页首都要增加一行:
  
  在编辑jsp页面时,一定要确保jsp文件以unicode的方式保存,目前几乎所有的编辑器都有以unicode编码保存或将文件内容转换成unicode的功能。
  
  2、增加一个用来声明request的characterencoding的类setcharacterencodingfilter.java;
  setcharacterencodingfilter的这个类主要的作用就是:把request在从页面刚提交到server端的时候的encoding声明为我们想要的encoding,通过调用request的方法setcharacterencoding (string encoding) 来改变,这样可以使request的从客户端传过来的时候,按我们在web.xml (在第二点可以讲到) 中配置的encoding来对提交的数据编码。
  
  3、修改web.xml文件,配置一个filter来过滤全部url请求,通过第二步中的类,声明所有url请求的编码类型未utf-8。
  在web.xml文件中加上以下这段:
  
  set character encoding
  org.kyle.web.sample.setcharacterencodingfilter
  
  encoding
  utf-8
  
  
  set character encoding
  /*
  
  
  在上面这段文字中“org.kyle.web.sample.setcharacterencodingfilter”指定步骤2中的类的位置,“ utf-8”指定我们希望声明的request的编码类型,“/*”指定这个filter的适用范围(这里指的是全部url请求)。
  
  同时注意二个问题:
  1:servlet的版本必需是支持request.setcharacterencoding(string encoding)这个方法才行,也就是在serlvert2.3以上。
  2:控制面板区域设置的当前代码页属性必需设定为"936 (gbk)",如果是"437(oem-united states)"它处理文字的时候是8-bit,而中文和日文等是16-bit。所以在显示和处理时它把中文的前8位给截掉,这样就会出现乱码问题。
  
  附:setcharacterencodingfilter源文件
  package org.kyle.web.sample;
  import java.io.ioexception;
  import javax.servlet.filter;
  import javax.servlet.filterchain;
  import javax.servlet.filterconfig;
  import javax.servlet.servletexception;
  import javax.servlet.servletrequest;
  import javax.servlet.servletresponse;
  import javax.servlet.unavailableexception;
  public class setcharacterencodingfilter implements filter
  {
  /**
  * the default character encoding to set for requests that pass through
  * this filter.
  */
  protected string encoding = null;
  
  /**
  * the filter configuration object we are associated with. if this value
  * is null, this filter instance is not currently configured.
  */
  protected filterconfig filterconfig = null;
  
  /**
  * should a character encoding specified by the client be ignored?
  */
  protected boolean ignore = true;
  
  /**
  * take this filter out of service.
  */
  public void destroy()
  {
  this.encoding = null;
  this.filterconfig = null;
  }
  
  /**
  * select and set (if specified) the character encoding to be used to
  * interpret request parameters for this request.
  *
  * @param request the servlet request we are processing
  * @param result the servlet response we are creating
  * @param chain the filter chain we are processing
  *
  * @exception ioexception if an input/output error occurs
  * @exception servletexception if a servlet error occurs
  */
  public void dofilter(servletrequest request, servletresponse response,
  filterchain chain)
  throws ioexception, servletexception
  {
  
  // conditionally select and set the character encoding to be used
  if (ignore || (request.getcharacterencoding() == null))
  {
  string encoding = selectencoding(request);
  if (encoding != null)
  request.setcharacterencoding(encoding);
  }
  
  // pass control on to the next filter
  chain.dofilter(request, response);
  }
  
  /**
  * place this filter into service.
  *
  * @param filterconfig the filter configuration object
  *
  *encoding
  * utf-8
  *
  */
  public void init(filterconfig filterconfig) throws servletexception
  {
  this.filterconfig = filterconfig;
  this.encoding = filterconfig.getinitparameter("encoding");
  string value = filterconfig.getinitparameter("ignore");
  if (value == null)
  this.ignore = true;
  else if (value.equalsignorecase("true"))
  this.ignore = true;
  else if (value.equalsignorecase("yes"))
  this.ignore = true;
  else
  this.ignore = false;
  }
  
  /**
  * select an appropriate character encoding to be used, based on the
  * characteristics of the current request and/or filter initialization
  * parameters. if no character encoding should be set, return
  * null.
  *
  
  * the default implementation unconditionally returns the value configured
  * by the encoding initialization parameter for this
  * filter.
  *
  * @param request the servlet request we are processing
  */
  protected string selectencoding(servletrequest request)
  {
  return (this.encoding);
  }
  }

扫描关注微信公众号