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

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

          原文地址

          這段時間經(jīng)常看到有人問到web開發(fā)中怎么中文總是?號。原因其實很簡單,因為大家大多用的是tomcat服務器,而tomcat服務器的默認編碼為 iso-8859-1(西歐字符)。就是因為iso-8859-1(西歐字符)編碼造成了我們經(jīng)常看到?號。關于iso-8859-1(西歐字符)更多知識請看《字節(jié),字符和編碼這篇文章。

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

          這個方法用于jsp頁面中的中文顯示。

          方法二:使用過濾器。
          過濾器使用主要針對表單提交,插入數(shù)據(jù)庫的數(shù)據(jù)都是?號。這也是應為tomcat不按request所指定的編碼進行編碼,還是自作主張的采用默認編碼方式iso-8859-1編碼。
          編寫一個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 自動生成方法存根
          if (ignore || (request.getCharacterEncoding() == null)) {
          String encoding = selectEncoding(request);
          if (encoding != null)
          request.setCharacterEncoding(encoding);
          }
          chain.doFilter(request, response);
          }

          public void destroy() {
          // TODO 自動生成方法存根
          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-->

          使用過濾器的好處很多,特別是項目之中。
          而且在使用國際化時就更有用了,只要在頁面指定<%@ page language="java" pageEncoding="UTF-8" %>,服務器就會根據(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>
          這個方法主要針對從url中獲取字符串的問題。
          在tomcat5.0及以上版本,post和get方法在處理編碼時有所不同。如果你在url中獲取中文就會出現(xiàn)?號。但在tomcat4.1版本沒有問題,因為tomcat4.1的post和get方法在處理編碼時是一樣的。

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

          Copyright © ikingqu

          主站蜘蛛池模板: 如皋市| 普兰县| 北海市| 涞水县| 黄骅市| 怀仁县| 军事| 蓬安县| 嵊泗县| 都安| 双柏县| 太原市| 娄底市| 宜宾县| 郯城县| 商丘市| 南木林县| 枣强县| 交口县| 沾化县| 宁阳县| 定陶县| 珲春市| 宜昌市| 宜春市| 松阳县| 宁城县| 镶黄旗| 石嘴山市| 大邑县| 株洲县| 开鲁县| 汉中市| 盐城市| 拉萨市| 龙游县| 湛江市| 新竹县| 兰考县| 永靖县| 根河市|