国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Java多語言編碼問題解析(2)

2019-11-18 13:14:14
字體:
供稿:網(wǎng)友

  這里是一個具體的例子:
  
  現(xiàn)在因為瀏覽器對UTF-8的支持,我們可以通過在源文件、請求、響應(yīng)中都使用unicode編碼方式,來輕松達到處理國際化和字符編碼問題的目標(biāo)。
  以我們使用的tomcat4.1.2為例,過程如下:
  
  1、編寫jsp頁面時:在每個JSP頁面在頁首都要增加一行:
  
  在編輯JSP頁面時,一定要確保JSP文件以unicode的方式保存,目前幾乎所有的編輯器都有以unicode編碼保存或?qū)⑽募?nèi)容轉(zhuǎn)換成unicode的功能。
  
  2、增加一個用來聲明request的CharacterEncoding的類SetCharacterEncodingFilter.java
  SetCharacterEncodingFilter的這個類主要的作用就是:把request在從頁面剛提交到server端的時候的encoding聲明為我們想要的encoding,通過調(diào)用request的方法setCharacterEncoding (String encoding) 來改變,這樣可以使request的從客戶端傳過來的時候,按我們在web.xml (在第二點可以講到) 中配置的encoding來對提交的數(shù)據(jù)編碼。
  
  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:控制面板區(qū)域設(shè)置的當(dāng)前代碼頁屬性必需設(shè)定為"936 (GBK)",假如是"437(OEM-United States)"它處理文字的時候是8-bit,而中文和日文等是16-bit。所以在顯示和處理時它把中文的前8位給截掉,這樣就會出現(xiàn)亂碼問題。
  
  附: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);
  }
  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西城区| 武冈市| 大渡口区| 绵阳市| 蕉岭县| 仪陇县| 吐鲁番市| 永寿县| 政和县| 大悟县| 静宁县| 时尚| 阿鲁科尔沁旗| 德兴市| 陕西省| 西畴县| 兴安盟| 方城县| 湖州市| 保德县| 新竹市| 湄潭县| 奈曼旗| 黑龙江省| 忻州市| 呼和浩特市| 南陵县| 东莞市| 盐源县| 义乌市| 宿州市| 伊金霍洛旗| 日喀则市| 甘泉县| 永嘉县| 报价| 双江| 东丰县| 新泰市| 大姚县| 古丈县|