Sung in Blog

                     一些技術文章 & 一些生活雜碎
          一、Include的頁面亂碼

            現象:include進來的頁面出現亂碼,其它頁面正常。

            原因:Tomcat在缺省情況下使用ISO-8859-1編碼,但是在include時有時Tomcat不能正確根據外層.jsp文件的編碼解析include進來的文件,造成include進來的文件中的中文亂碼。

            解決:這兒可以有很多解決辦法,但是對于我們的中文環境,從根本上的解決辦法是將Tomcat 5.0.19的核心缺省編碼從ISO-8859-1修改為GBK ??梢栽谙旅娴刂废螺d修改過的jar文件,

            jasper-compiler.jar,jasper-runtime.jar位于/common/lib下,其它位于/server/lib下,將新的.jar文件替代原.jar即可。
            
            二、提交的數據亂碼

            現象:通過表單提交的數據出現亂碼。

            原因:原因未明??赡苁荰omcat在接收到請求后,并沒有能夠根據request中的信息提前正確的編碼方式。

            解決:可以添加一個設置字符集的Filter。

          package filters;

          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;
          import javax.servlet.UnavailableException;

          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 {

           // Conditionally select and set the character encoding to be used
           if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
             request.setCharacterEncoding(encoding);
           }

           // Pass control on to the next filter
           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);
          }

          }

            配置web.xml

          <filter>
          ?。糵ilter-name>Set Character Encoding</filter-name>
           <filter-class>filters.SetCharacterEncodingFilter</filter-class>
           <init-param>
            <param-name>encoding</param-name>
           ?。紁aram-value>GBK</param-value>
          ?。?init-param>
          </filter>

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

            現象:include進來的頁面出現亂碼,其它頁面正常。

            原因:Tomcat在缺省情況下使用ISO-8859-1編碼,但是在include時有時Tomcat不能正確根據外層.jsp文件的編碼解析include進來的文件,造成include進來的文件中的中文亂碼。

            解決:這兒可以有很多解決辦法,但是對于我們的中文環境,從根本上的解決辦法是將Tomcat 5.0.19的核心缺省編碼從ISO-8859-1修改為GBK ??梢栽谙旅娴刂废螺d修改過的jar文件,

            jasper-compiler.jar,jasper-runtime.jar位于/common/lib下,其它位于/server/lib下,將新的.jar文件替代原.jar即可。
            
            二、提交的數據亂碼

            現象:通過表單提交的數據出現亂碼。

            原因:原因未明??赡苁荰omcat在接收到請求后,并沒有能夠根據request中的信息提前正確的編碼方式。

            解決:可以添加一個設置字符集的Filter。

          package filters;

          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;
          import javax.servlet.UnavailableException;

          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 {

           // Conditionally select and set the character encoding to be used
           if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
             request.setCharacterEncoding(encoding);
           }

           // Pass control on to the next filter
           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);
          }

          }

            配置web.xml

          <filter>
          ?。糵ilter-name>Set Character Encoding</filter-name>
          ?。糵ilter-class>filters.SetCharacterEncodingFilter</filter-class>
           <init-param>
           ?。紁aram-name>encoding</param-name>
           ?。紁aram-value>GBK</param-value>
           </init-param>
          </filter>

          <filter-mapping>
           <filter-name>Set Character Encoding</filter-name>
           <url-pattern>/*</url-pattern>
          </filter-mapping>
          ]]>
          posted on 2005-09-02 10:09 Sung 閱讀(429) 評論(0)  編輯  收藏 所屬分類: Tomcat
          主站蜘蛛池模板: 长子县| 广平县| 神农架林区| 托克逊县| 广饶县| 九龙城区| 密山市| 湖南省| 武山县| 会宁县| 民乐县| 南阳市| 赤壁市| 汝城县| 青浦区| 昌平区| 远安县| 宜阳县| 汉沽区| 鄱阳县| 武山县| 吉林省| 清涧县| 吉木萨尔县| 清新县| 河北省| 赣榆县| 安吉县| 邢台市| 永清县| 诏安县| 蒙城县| 玉环县| 东海县| 鄂托克旗| 镶黄旗| 玛曲县| 塘沽区| 江川县| 津市市| 承德县|