posts - 2,  comments - 0,  trackbacks - 0

          I want to say that it is too boring for the diffrent character rule such as ISO-8656-1,UTF-8,GB2312 . I find I always can't use them very facility. I find a article how to solve character trouble. Actually the problem in jsp or servlet is as three aspact:

              

          解決JSP開發Web程序中的中文問題

            這段時間經??吹接腥藛柕絯eb開發中怎么中文總是?號。原因其實很簡單,因為大家大多用的是tomcat服務器,而tomcat服務器的默認編碼為 iso-8859-1(西歐字符)。就是因為iso-8859-1(西歐字符)編碼造成了我們經??吹剑刻?。

            方法一:最簡單也是用的最多的方法。

           ?。?@ page language="java" pageEncoding="GBK" %>

            或者<%@ page contenttype="text/html;charset=gbk";>這里可以用gb2312或者gbk,只是gbk比gb2312支持跟多的字符。

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

            方法二:使用過濾器。

            過濾器使用主要針對表單提交,插入數據庫的數據都是?號。這也是應為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>
          ?。糵ilter-name>Set Character Encoding</filter-name>
          ?。糵ilter-class>com.struts.common.SetCharacterEncodingFilter</filter-class>
          ?。糹nit-param>
            <param-name>encoding</param-name>
           ?。紁aram-value>UTF-8</param-value>
          ?。?init-param>
          </filter>

          <filter-mapping>
           <filter-name>Set Character Encoding</filter-name>
           ?。紆rl-pattern>/*</url-pattern>
          ?。?filter-mapping>
          <!-- Set Character Encoding-->

            使用過濾器的好處很多,特別是項目之中。

            而且在使用國際化時就更有用了,只要在頁面指定 <%@ page language="java" pageEncoding="UTF-8" %>,服務器就會根據本地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中獲取中文就會出現?號。但在tomcat4.1版本沒有問題,因為tomcat4.1的post和get方法在處理編碼時是一樣的。

          posted on 2006-03-14 09:38 Felix 閱讀(100) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蒙阴县| 桐城市| 兰溪市| 剑阁县| 广汉市| 太仓市| 曲靖市| 长治县| 大足县| 侯马市| 聊城市| 贺州市| 绥宁县| 灵川县| 宜阳县| 睢宁县| 德惠市| 精河县| 连山| 久治县| 米林县| 治县。| 兴国县| 肃南| 大关县| 东台市| 华安县| 三原县| 菏泽市| 申扎县| 湖口县| 曲靖市| 南陵县| 湄潭县| 房产| 义马市| 仪陇县| 玛多县| 环江| 万全县| 福清市|