<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實現(xiàn)編碼轉(zhuǎn)化。

    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響應的字符轉(zhuǎn)化為UTF-8的編碼了


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

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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 久久久久免费看黄a级试看| 免费人人潮人人爽一区二区| AAA日本高清在线播放免费观看| 区三区激情福利综合中文字幕在线一区亚洲视频1| 亚洲av永久无码精品三区在线4 | 成人啪精品视频免费网站| 亚洲第一页中文字幕| 久久久久久精品免费看SSS| 中文字幕亚洲色图| 97视频免费在线| 亚洲欧美成人综合久久久| 日韩电影免费在线| 少妇亚洲免费精品| 亚洲精品少妇30p| 国产亚洲色婷婷久久99精品| 免费国产成人午夜在线观看| 久久亚洲国产成人精品性色| 男女免费观看在线爽爽爽视频| 亚洲欧美日本韩国| 又大又黄又粗又爽的免费视频| eeuss草民免费| 亚洲精品福利视频| 成人免费视频软件网站| 国产亚洲综合视频| 精品亚洲一区二区| 亚洲视频免费播放| 欧洲亚洲综合一区二区三区| 久久精品夜色噜噜亚洲A∨| 色噜噜噜噜亚洲第一| 不卡一卡二卡三亚洲| 99久久99热精品免费观看国产| 亚洲日本VA午夜在线影院| 亚洲午夜福利精品无码| 13一14周岁毛片免费| 羞羞视频在线观看免费| 亚洲经典在线中文字幕| 国产男女猛烈无遮挡免费网站| 日韩视频在线观看免费| 亚洲av永久无码天堂网| 亚洲国产第一站精品蜜芽| 成人免费午夜视频|