<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 11  文章 - 11  trackbacks - 0

    留言簿(2)

    隨筆分類(13)

    隨筆檔案(11)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    Web程序的編碼問題主要有三個方面:

    1. 程序文件的編碼;
    2. 輸出頁面到客戶端的編碼;
    3. 用戶響應到服務器端的編碼。

    以設置為UTF-8為例,可以用如下方法解決。

    1. 對于程序文件的編碼

    直接在Eclipse或者其他IDE,editor中將文件編碼設為UTF-8即可。

    2.輸出頁面

    對于JSP頁面,加入以下代碼

    <%@ page contentType="text/html; charset=UTF-8"%>
    如果在IE或者Firefox中還不能正常顯示,還可以在Html標識下加入下面的頭信息

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    3. 響應到服務器端

    我使用的是Tomcat,沒有可以直接設置接受響應編碼的方法(真是太佩服了!),但可以通過寫一個Filter實現編碼轉化。

    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 destroy() {
            this.encoding = null;
            this.filterConfig = null;
        }
    
        public void doFilter(ServletRequest request, ServletResponse response,
                              FilterChain chain)
            throws IOException, ServletException {
            if (ignore || (request.getCharacterEncoding() == null)) {
                String encoding = selectEncoding(request);
                if (encoding != null)
                    request.setCharacterEncoding(encoding);
            }
            chain.doFilter(request, response);
        }
    
        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;
        }
    
        protected String selectEncoding(ServletRequest request) {
            return (this.encoding);
        }
    }
    

    將SetCharacterEncodingFilter放到程序的編譯目錄下,再在web.xml中添加相應屬性

    
        <filter>
            <filter-name>Set Character Encoding</filter-name>
            <filter-class>hijeff.filters.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>    
    

    Tomcat就會將用戶通過HTTP響應的字符轉化為UTF-8的編碼了


    posted on 2007-03-15 20:58 hijeff 閱讀(308) 評論(0)  編輯  收藏 所屬分類: Tomcat

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


    網站導航:
     
    主站蜘蛛池模板: 免费国产人做人视频在线观看| 国产成人免费高清激情明星| 久久久久亚洲AV无码专区首| 特级毛片全部免费播放| 日本免费网站观看| 亚洲av无码av在线播放| 国产精品久久免费视频| 国产精品久久久久久亚洲小说| 波多野结衣中文一区二区免费| 国产亚洲欧美在线观看| 亚洲VA综合VA国产产VA中| 五月婷婷免费视频| 亚洲va中文字幕无码久久不卡| 日韩精品无码免费专区午夜 | 特黄aa级毛片免费视频播放| 国产一区二区三区在线免费观看| 猫咪www免费人成网站| 亚洲精品少妇30p| 国产精品区免费视频| 亚洲男女性高爱潮网站| 夫妻免费无码V看片| 天堂亚洲免费视频| 亚洲s色大片在线观看| 真人做人试看60分钟免费视频 | 亚洲视频免费观看| 日本亚洲免费无线码| 亚洲精品色午夜无码专区日韩| 国产午夜免费高清久久影院| 亚洲色欲久久久久综合网| 拍拍拍无挡免费视频网站| 亚洲午夜视频在线观看| 女人18毛片水真多免费看 | 亚洲AV无码欧洲AV无码网站| 亚州免费一级毛片| 激情婷婷成人亚洲综合| 亚洲人色婷婷成人网站在线观看 | 麻豆国产人免费人成免费视频| 成年网站免费入口在线观看| 亚洲人成网站影音先锋播放| 成年在线观看免费人视频草莓| 一个人看的免费高清视频日本|