国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

tomcat中文問題--過濾器

2019-11-17 06:17:18
字體:
供稿:網(wǎng)友

 

轉(zhuǎn)載請注明:http://www.csdn.net/develop/article/17/17204.shtm

作者:ggyy1977@hotmail.com   

                                   使用filter來改變request的編碼

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

知道當(dāng)沒有指定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);///有機(jī)會執(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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁晋县| 邓州市| 桐乡市| 双江| 海原县| 榆林市| 马尔康县| 盐池县| 海南省| 兴业县| 龙州县| 临城县| 叶城县| 平和县| 永福县| 博罗县| 梨树县| 阳春市| 东台市| 瓦房店市| 东宁县| 德保县| 巴里| 广西| 汤原县| 渝中区| 汉中市| 南溪县| 延寿县| 商都县| 科技| 灵山县| 壶关县| 阳新县| 泾川县| 遵化市| 乌鲁木齐县| 曲靖市| 石屏县| 吴川市| 大田县|