Kimi's NutShell

          我荒廢的今日,正是昨日殞身之人祈求的明日

          BlogJava 新隨筆 管理
            141 Posts :: 0 Stories :: 75 Comments :: 0 Trackbacks

          使用filter來改變request的編碼.

          當沒有指定request的編碼的時候,從客戶端得到的數(shù)據(jù)是iso-8859-1編碼的(request.getParameter()得到傳遞的參數(shù)值);
          但是我們怎么來改變request的編碼呢?


          方法有很多種!


          比如:在getRequestDispatcher("/jsp/jsptoserv/hello.jsp").forward(request, response);之前修改


          request的編碼,那么在jsp/jsptoserv/hello.jsp中得到的參數(shù)值就是制定的編碼的字符。


          本文我們使用Filter來修改request的編碼

          1)首先編寫filter類:
          package myFilter;
          import java.io.IOException;
          import javax.servlet.*;
          public class ChangeCharsetFilter implements Filter {
          protected String encoding = null;/////要制定的編碼,在web.xml中配置
          protected FilterConfig filterConfig = null;
          public void destroy() {
          this.encoding = null;
          this.filterConfig = null;
          }
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          throws IOException, ServletException {
          if (request.getCharacterEncoding() == null){
          ?String encoding = getEncoding();////得到指定的編碼名字
          ?if (encoding != null)
          ?request.setCharacterEncoding(encoding);////設(shè)置request的編碼
          ?}
          chain.doFilter(request, response);///有機會執(zhí)行下一個filter
          }
          ?public void init(FilterConfig filterConfig) throws ServletException {
          ?this.filterConfig = filterConfig;
          this.encoding = filterConfig.getInitParameter("encoding");///得到在web.xml中配置的編碼
          ?}
          ?protected String getEncoding() {
          ?return (this.encoding);///得到指定的編碼
          }

          }

          2。編輯web.xml文件
          <?xml version="1.0" encoding="ISO-8859-1"?>
          <!DOCTYPE web-app
          ? PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          ? "<web-app>
          <filter>
          <filter-name>SetCharacterEncoding</filter-name>
          ?<filter-class>myFilter.ChangeCharsetFilter </filter-class>
          ?<init-param>
          ?<param-name>encoding</param-name>
          <param-value>GB2312</param-value>//////指定編碼為GB2312
          </init-param>
          ?</filter>
          <filter-mapping>
          ?<filter-name>SetCharacterEncoding</filter-name>
          <url-pattern>/*</url-pattern>////////對于所有的request改變其編碼
          </filter-mapping>
          </web-app>
          ///
          3。寫一個a.jsp


          <%@ page contentType="text/html; charset=GB2312" %>
          <html>
          <head></head>
          <body>
          <%
          String name=request.getParameter("name");///本來這里得到字符是iso-8859-1編碼的,不能直接


          在Console中輸出的,但是現(xiàn)在改變了request的編碼方式,此時的name的編碼是GB2312,所以能正確在Console中顯示的。

          System.out.println(name);


          %>
          <form action="a.jsp" method="post">
          <input type="text" name="name">
          <input type="submit">
          </form>
          <%=name%>
          </body>
          </html>


          完!


          關(guān)于中文處理的問題就寫這些了!


          評論

          # re: 通過filter類對get方式上來的中文進行統(tǒng)一編碼處理 2008-02-16 17:16 aqiu
          被你捷足先登了,前段時間剛看的  回復(fù)  更多評論
            

          # re: 通過filter類對get方式上來的中文進行統(tǒng)一編碼處理[未登錄] 2008-02-18 08:42 Kimi
          嘿嘿  回復(fù)  更多評論
            

          主站蜘蛛池模板: 兴和县| 政和县| 广南县| 桑日县| 濮阳市| 伊吾县| 霍城县| 肥乡县| 乌兰察布市| 揭西县| 盐津县| 盐亭县| 东阿县| 南昌县| 故城县| 武山县| 湖口县| 商南县| 宝山区| 凯里市| 南通市| 浪卡子县| 广水市| 彰化县| 临泉县| 长寿区| 白河县| 吴桥县| 安达市| 宁陵县| 龙南县| 定边县| 花莲市| 临邑县| 西平县| 嵊泗县| 西青区| 马关县| 南通市| 牟定县| 平原县|