麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

Java多語(yǔ)言編碼問(wèn)題解析(2)

2019-11-18 13:14:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  這里是一個(gè)具體的例子:
  
  現(xiàn)在因?yàn)闉g覽器對(duì)UTF-8的支持,我們可以通過(guò)在源文件、請(qǐng)求、響應(yīng)中都使用unicode編碼方式,來(lái)輕松達(dá)到處理國(guó)際化和字符編碼問(wèn)題的目標(biāo)。
  以我們使用的tomcat4.1.2為例,過(guò)程如下:
  
  1、編寫jsp頁(yè)面時(shí):在每個(gè)JSP頁(yè)面在頁(yè)首都要增加一行:
  
  在編輯JSP頁(yè)面時(shí),一定要確保JSP文件以u(píng)nicode的方式保存,目前幾乎所有的編輯器都有以u(píng)nicode編碼保存或?qū)⑽募?nèi)容轉(zhuǎn)換成unicode的功能。
  
  2、增加一個(gè)用來(lái)聲明request的CharacterEncoding的類SetCharacterEncodingFilter.java
  SetCharacterEncodingFilter的這個(gè)類主要的作用就是:把request在從頁(yè)面剛提交到server端的時(shí)候的encoding聲明為我們想要的encoding,通過(guò)調(diào)用request的方法setCharacterEncoding (String encoding) 來(lái)改變,這樣可以使request的從客戶端傳過(guò)來(lái)的時(shí)候,按我們?cè)趙eb.xml (在第二點(diǎn)可以講到) 中配置的encoding來(lái)對(duì)提交的數(shù)據(jù)編碼。
  
  3、修改web.xml文件,配置一個(gè)filter來(lái)過(guò)濾全部url請(qǐng)求,通過(guò)第二步中的類,聲明所有url請(qǐng)求的編碼類型未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的編碼類型,“/*”指定這個(gè)filter的適用范圍(這里指的是全部url請(qǐng)求)。
  
  同時(shí)注重二個(gè)問(wèn)題:
  1:servlet的版本必需是支持request.setCharacterEncoding(String encoding)這個(gè)方法才行,也就是在serlvert2.3以上。
  2:控制面板區(qū)域設(shè)置的當(dāng)前代碼頁(yè)屬性必需設(shè)定為"936 (GBK)",假如是"437(OEM-United States)"它處理文字的時(shí)候是8-bit,而中文和日文等是16-bit。所以在顯示和處理時(shí)它把中文的前8位給截掉,這樣就會(huì)出現(xiàn)亂碼問(wè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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 日本一区二区不卡在线观看 | 久久国产精品影视 | 奶子吧naiziba.cc免费午夜片在线观看 | 国产精品久久久久免费视频 | 国产精品一区二区手机在线观看 | 超污视频在线看 | 国产成人av一区 | xxxx69hd一hd| 黄污免费网站 | 永久av在线免费观看 | 亚洲码无人客一区二区三区 | 性盈盈盈影院 | 蜜桃一本色道久久综合亚洲精品冫 | 中文字幕在线观看视频www | 国产亚洲精品美女久久久 | 国产小视频在线观看 | 亚洲午夜影院在线观看 | 视频一区二区在线播放 | 亚洲最新色 | 一级毛片在线观看视频 | 91精品国产91久久久久久 | 国产精品一区二区三区在线看 | 精品国产一区二区三区四区阿崩 | 蜜桃网在线 | a黄在线观看 | 免费a视频在线观看 | 国产午夜精品久久久久久免费视 | 九九午夜 | 国产精品视频海角社区88 | 国产精品9191 | av在线播放网址 | 国内精品免费一区二区2001 | 午夜神马福利视频 | 国产欧美亚洲精品 | 成人在线a | 久久美女免费视频 | 国产精品视频中文字幕 | 特级西西444www大精品视频免费看 | 精品一区二区三区免费毛片爱 | 一区二区三区国产在线 | 国产99久久久久久免费看农村 |