[收藏]解決web開發(fā)中的中文問題

          Posted on 2006-03-16 00:49 ikingqu 閱讀(388) 評(píng)論(0)  編輯  收藏 所屬分類: Others

          原文地址

          這段時(shí)間經(jīng)常看到有人問到web開發(fā)中怎么中文總是?號(hào)。原因其實(shí)很簡(jiǎn)單,因?yàn)榇蠹掖蠖嘤玫氖莟omcat服務(wù)器,而tomcat服務(wù)器的默認(rèn)編碼為 iso-8859-1(西歐字符)。就是因?yàn)閕so-8859-1(西歐字符)編碼造成了我們經(jīng)常看到?號(hào)。關(guān)于iso-8859-1(西歐字符)更多知識(shí)請(qǐng)看《字節(jié),字符和編碼這篇文章。

          方法一:最簡(jiǎn)單也是用的最多的方法。
          <%@ page language="java" pageEncoding="GBK" %>
          或者<%@ page contenttype="text/html;charset=gbk";>這里可以用gb2312或者gbk,只是gbk比gb2312支持跟多的字符。

          這個(gè)方法用于jsp頁面中的中文顯示。

          方法二:使用過濾器。
          過濾器使用主要針對(duì)表單提交,插入數(shù)據(jù)庫的數(shù)據(jù)都是?號(hào)。這也是應(yīng)為tomcat不按request所指定的編碼進(jìn)行編碼,還是自作主張的采用默認(rèn)編碼方式iso-8859-1編碼。
          編寫一個(gè)SetCharacterEncodingFilter類。
          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;

          public class SetCharacterEncodingFilter implements Filter {
          protected String encoding = null;
          protected FilterConfig filterConfig = null;
          protected boolean ignore = true;

          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
          this.ignore=false;
          }

          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
          // TODO 自動(dòng)生成方法存根
          if (ignore || (request.getCharacterEncoding() == null)) {
          String encoding = selectEncoding(request);
          if (encoding != null)
          request.setCharacterEncoding(encoding);
          }
          chain.doFilter(request, response);
          }

          public void destroy() {
          // TODO 自動(dòng)生成方法存根
          this.encoding = null;
          this.filterConfig = null;
          }

          protected String selectEncoding(ServletRequest request) {
          return (this.encoding);
          }
          }

          然后再web.xml加上
          <!-- Set Character Encoding-->
          <filter>
          <filter-name>Set Character Encoding</filter-name>
          <filter-class>com.struts.common.SetCharacterEncodingFilter</filter-class>
          <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
          </init-param>
          </filter>

          <filter-mapping>
          <filter-name>Set Character Encoding</filter-name>
          <url-pattern>/*</url-pattern>
          </filter-mapping>
          <!-- Set Character Encoding-->

          使用過濾器的好處很多,特別是項(xiàng)目之中。
          而且在使用國際化時(shí)就更有用了,只要在頁面指定<%@ page language="java" pageEncoding="UTF-8" %>,服務(wù)器就會(huì)根據(jù)本地Locale來顯示正確的字符集。

          所以我特別推薦使用過濾器。

          方法三:修改tomcat的server.xml文件中URIEncoding。
          <Connector
          debug="0"
          acceptCount="100"
          connectionTimeout="20000"
          disableUploadTimeout="true"
          port="80"
          redirectPort="8443"
          enableLookups="false"
          minSpareThreads="25"
          maxSpareThreads="75"
          maxThreads="150"
          maxPostSize="0"
          URIEncoding="GBK"
          >
          </Connector>
          這個(gè)方法主要針對(duì)從url中獲取字符串的問題。
          在tomcat5.0及以上版本,post和get方法在處理編碼時(shí)有所不同。如果你在url中獲取中文就會(huì)出現(xiàn)?號(hào)。但在tomcat4.1版本沒有問題,因?yàn)閠omcat4.1的post和get方法在處理編碼時(shí)是一樣的。

          posts - 4, comments - 5, trackbacks - 0, articles - 60

          Copyright © ikingqu

          主站蜘蛛池模板: 淮阳县| 武安市| 班玛县| 清新县| 新余市| 徐汇区| 乳源| 西畴县| 虹口区| 岫岩| 和平县| 南投县| 巴彦淖尔市| 调兵山市| 泰来县| 安宁市| 图们市| 西安市| 疏附县| 通化县| 顺昌县| 仁化县| 宣汉县| 越西县| 沅陵县| 阳春市| 当阳市| 三门县| 左云县| 庄河市| 北票市| 内丘县| 无极县| 崇信县| 红桥区| 平邑县| 乌苏市| 赤峰市| 丹巴县| 台安县| 衡南县|