熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

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

2013-11-15 11:49:49  來源: JSP教程 

  這裡是一個具體的例子
  
  現在因為浏覽器對UTF的支持我們可以通過在源文件請求響應中都使用unicode編碼方式來輕松達到處理國際化和字符編碼問題的目標
  以我們使用的tomcat為例過程如下
  
  編寫JSP頁面時在每個JSP頁面在頁首都要增加一行
  
  在編輯JSP頁面時一定要確保JSP文件以unicode的方式保存目前幾乎所有的編輯器都有以unicode編碼保存或將文件內容轉換成unicode的功能
  
  增加一個用來聲明request的CharacterEncoding的類SetCharacterEncodingFilterjava
  SetCharacterEncodingFilter的這個類主要的作用就是把request在從頁面剛提交到server端的時候的encoding聲明為我們想要的encoding通過調用request的方法setCharacterEncoding (String encoding) 來改變這樣可以使request的從客戶端傳過來的時候按我們在webxml (在第二點可以講到) 中配置的encoding來對提交的數據編碼
  
  修改webxml文件配置一個filter來過濾全部url請求通過第二步中的類聲明所有url請求的編碼類型未UTF
  在webxml文件中加上以下這段
  
  Set Character Encoding
  orgkylewebsampleSetCharacterEncodingFilter
  
  encoding
  UTF
  
  
  Set Character Encoding
  /*
  
  
  在上面這段文字中orgkylewebsampleSetCharacterEncodingFilter指定步驟中的類的位置 UTF指定我們希望聲明的request的編碼類型/*指定這個filter的適用范圍(這裡指的是全部url請求)
  
  同時注意二個問題
  servlet的版本必需是支持requestsetCharacterEncoding(String encoding)這個方法才行也就是在serlvert以上
  控制面板區域設置的當前代碼頁屬性必需設定為 (GBK)如果是(OEMUnited States)它處理文字的時候是-bit而中文和日文等是-bit所以在顯示和處理時它把中文的前位給截掉這樣就會出現亂碼問題
  
  附SetCharacterEncodingFilter源文件
  package orgkylewebsample;
  import javaioIOException;
  import javaxservletFilter;
  import javaxservletFilterChain;
  import javaxservletFilterConfig;
  import javaxservletServletException;
  import javaxservletServletRequest;
  import javaxservletServletResponse;
  import javaxservletUnavailableException;
  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()
  {
  thisencoding = null;
  thisfilterConfig = 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 || (requestgetCharacterEncoding() == null))
  {
  String encoding = selectEncoding(request);
  if (encoding != null)
  requestsetCharacterEncoding(encoding);
  }
  
  // Pass control on to the next filter
  chaindoFilter(request response);
  }
  
  /**
  * Place this filter into service
  *
  * @param filterConfig The filter configuration object
  *
  *encoding
  * UTF
  *
  */
  public void init(FilterConfig filterConfig) throws ServletException
  {
  thisfilterConfig = filterConfig;
  thisencoding = filterConfiggetInitParameter(encoding);
  String value = filterConfiggetInitParameter(ignore);
  if (value == null)
  thisignore = true;
  else if (valueequalsIgnoreCase(true))
  thisignore = true;
  else if (valueequalsIgnoreCase(yes))
  thisignore = true;
  else
  thisignore = 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 (thisencoding);
  }
  }

From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19699.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.