Java,J2EE,Weblogic,Oracle

          java項目隨筆
          隨筆 - 90, 文章 - 6, 評論 - 61, 引用 - 0
          數據加載中……

          java過濾器的小技巧

          <%@ page language="java" pageEncoding="GB2312"%>  與
          <%@ page contentType="text/html; charset=GB2312" pageEncoding="GB2312" language="java" errorPage=""%>區別:


          1.過濾器servlet
          public class CharsetFilter
              implements Filter
          {

              protected String encoding;
              protected FilterConfig filterConfig;
              protected boolean ignore;

              public CharsetFilter()
              {
                  encoding = null;
                  filterConfig = null;
                  ignore = true;
              }

              public void destroy()
              {
                  encoding = null;
                  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;
                  encoding = filterConfig.getInitParameter("encoding");
                  System.out.println("11111111111:"+encoding);
                  String value = filterConfig.getInitParameter("ignore");
                  if(value == null)
                      ignore = true;
                  else
                  if(value.equalsIgnoreCase("true"))
                      ignore = true;
                  else
                  if(value.equalsIgnoreCase("yes"))
                      ignore = true;
                  else
                      ignore = false;
              }

              protected String selectEncoding(ServletRequest request)
              {
                  return encoding;
              }
          }

          2.在 web.xml 配置
           <filter>
            <filter-name>CharsetFilter</filter-name>
            <filter-class>com.easyjf.web.CharsetFilter</filter-class>
            <init-param>
             <param-name>encoding</param-name>
             <param-value>GB2312</param-value>
            </init-param>
            <init-param>
             <param-name>ignore</param-name>
             <param-value>true</param-value>
            </init-param>
           </filter>
           <filter-mapping>
            <filter-name>CharsetFilter</filter-name>
            <servlet-name>action</servlet-name>
           </filter-mapping>

          小結:
          在使用<%@ page language="java" pageEncoding="GB2312"%> 顯示的是亂碼。
          在使用<%@ page contentType="text/html; charset=GB2312" language="java" pageEncoding="GB2312"%>卻是正常的。


          1.jsp :
                  <%@ page contentType="text/html; charset=gb2312" language="java">
                 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

          2.web.xml中配置 gb2312

          posted on 2006-11-29 10:08 龔椿深 閱讀(1144) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 保定市| 大理市| 遂宁市| 东丰县| 灵寿县| 利辛县| 雅江县| 海兴县| 中牟县| 永济市| 辽源市| 镇平县| 汝州市| 宝应县| 芜湖市| 呼伦贝尔市| 习水县| 新闻| 内江市| 茂名市| 兰溪市| 吕梁市| 延长县| 临邑县| 慈溪市| 五家渠市| 乃东县| 资溪县| 贺兰县| 重庆市| 柳林县| 卫辉市| 姚安县| 霍州市| 西华县| 汝城县| 海口市| 陕西省| 津南区| 元氏县| 大港区|