用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - jsp代码库

jsp 过滤器防止中文表单提交乱码

2013-04-02 作者: 小蜜锋举报

[jsp]代码库

package com.gtxy.dynaActionForm.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 以 UTF-8 输入输出
*
* @author ZhongHuiQiang
*
*/
public class FilterChar implements Filter {

    private String config;

    public void destroy() {
        this.config = null;
    }

    /**
      * name="FilterChar" <br>
      * init-param name="config" value="utf-8"<br>
      * mapping url-pattern="/*"<br>
      */
    public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req;
        req = (HttpServletRequest) request;

        HttpServletResponse res;
        res = (HttpServletResponse) response;

        req.setCharacterEncoding (config);
        res.setCharacterEncoding (config);
        chain.doFilter (req, res);

    }

    /**
      * 获得web.xml中初始化的参数
      */
    public void init (FilterConfig config) throws ServletException {
        this.config = config.getInitParameter ("chars");
    }

}




------------------------------

<filter>
    <filter-name>FilterChar</filter-name>
    <filter-class>
     com.gtxy.dynaActionForm.web.filter.FilterChar
    </filter-class>
    <init-param>
      <param-name>chars</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>FilterChar</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...