| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 805 人关注过本帖
标题:servlet过滤中文字符
只看楼主 加入收藏
雪融寒
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-7-3
收藏
 问题点数:0 回复次数:0 
servlet过滤中文字符
每个大型的j2ee都要用到过滤中文字符的过滤器

 

package filter;

import

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

 

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 {

        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;
  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>
      <filter-name>Set Character Encoding</filter-name>
      <filter-class>filter.SetCharacterEncodingFilter</filter-class>
      <init-param>
      <param-name>encoding</param-name>
         <param-value>UTF-8</param-value>
      </init-param>
   </filter>
   <filter-mapping>
      <filter-name>Set Character Encoding</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

即可

其中<url-pattern>/*</url-pattern>
表示对所有请求都要过滤,如果改成

<url-pattern>/*.do</url-pattern>表示只对.do结尾的请求过滤

如果改成

<url-pattern>/a.jsp</url-pattern> 表示只对a.jsp这个页面过滤

具体问题再 具体分析

 

zonecens     www.
搜索更多相关主题的帖子: servlet 中文字符 import 过滤器 javax 
2008-03-09 16:43
快速回复:servlet过滤中文字符
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031321 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved