• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    tomcat中文問題--過濾器

    發布: 2007-6-22 07:38 | 作者:   | 來源:   | 查看: 24次 | 進入軟件測試論壇討論

    領測軟件測試網

       

     

    轉載請注明:http://www.csdn.net/develop/article/17/17204.shtm

    作者:ggyy1977@hotmail.com   

                                       使用filter來改變request的編碼

    在前面的文章里面,我們討論了在tomcat下的jsp和servlet的字符編碼問題!

    知道當沒有指定request的編碼的時候,從客戶端得到的數據是iso-8859-1編碼的(request.getParameter()得到傳遞的參數值);

    但是我們怎么來改變request的編碼呢?

    方法有很多種!

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

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

    本文我們使用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);////設置request的編碼
            }

             chain.doFilter(request, response);///有機會執行下一個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"
     "http://java.sun.com/dtd/web-app_2_3.dtd">

    <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中輸出的,但是現在改變了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>

    完!

    關于中文處理的問題就寫這些了!

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>