JSP过滤器(Filter)之实现全站解决中文提交乱码(包括get提交方式)
来源:互联网 发布:淘宝联盟隐藏计划 编辑:程序博客网 时间:2024/05/21 09:09
/*对所有乱码进行拦截及纠正,包括get提交方式,解决get提交方式乱码,方式是重写了request的getParameter方法,让其在返回值的时候检查提交方式,如果为“get”就进行解码 */public class FilterTest1 implements Filter{private String code = null;@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {//设置码表request.setCharacterEncoding(code);response.setCharacterEncoding(code);response.setContentType("text/html;charset="+code);//将现在的reuqest对象换成我自己的request对象MyHttpServletRequest myRequest = new MyHttpServletRequest((HttpServletRequest)request);//跳转到请求的资源,并传入自己定义的myRequest对象chain.doFilter(myRequest, response);}@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void init(FilterConfig config) throws ServletException {// TODO Auto-generated method stub//获取配置的编码格式String code = config.getInitParameter("code");//获取xml文件<init-param>标签中code的value//如果没有配置,就使用UTF-8默认编码this.code = code == null || "".equals(code.trim()) ? "utf-8" : code;}}//重新定义一个自己的request对象,继承HttpServletRequestWrapper 这个HttpServletRequest的适配器class MyHttpServletRequest extends HttpServletRequestWrapper{public MyHttpServletRequest(HttpServletRequest request){super(request);}//重写getParameter方法@Overridepublic String getParameter(String name) {String value = super.getParameter(name);if(value == null)return null;//判断只要不是“get”方式提交就直接返回,equalsIgnoreCase方法是不考虑大小写的匹配if(!"get".equalsIgnoreCase(super.getMethod()))return value;//如果请求方式为"get",那么就进行加码,解码码表为父类的码表try {value = new String(value.getBytes("ISO-8859-1"),super.getCharacterEncoding());} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return value;}}
在web.xml配置文件中 注册此过滤器
<!-- 注册过滤器(ServletTest1.jsp的过滤器) --><filter><filter-name>FilterTest1</filter-name><filter-class>cn.messy_code.FilterTest1</filter-class><!-- 设置init方法接收的参数(配置的是编码表) --><init-param><param-name>code</param-name><param-value>UTF-8</param-value></init-param></filter><!-- 当访问此web应用中所有的资源时,都用过滤器拦截 --> <filter-mapping> <filter-name>FilterTest1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
0 0
- JSP过滤器(Filter)之实现全站解决中文提交乱码(包括get提交方式)
- 过滤器解决get和post方式提交的中文乱码
- 中文乱码问题解决,过滤器配置,get post提交乱码,filter,struts乱码,jsp乱码
- JSP中Get提交方式的中文乱码解决
- 用过滤器解决全站中文数据提交乱码问题
- jsp get提交方式中文乱码解决方案
- Post与Get提交使用过滤器解决中文乱码问题
- Get提交中文乱码解决
- get方式提交乱码解决
- 解决jsp页面传递通过get方式提交中文到后台乱码的问题
- jsp中Get提交方式的中文乱码解决办法
- jsp中Get提交方式的中文乱码解决办法
- jsp中Get提交方式的中文乱码解决办法
- jsp中Get提交方式的中文乱码解决办法
- jsp中Get提交方式的中文乱码解决办法
- jsp表单提交(post提交方式)中文乱码解决方法
- 解决表单get方式提交时的中文乱码问题
- 解决ThinkPHP中以Get方式提交中文字符乱码
- NYOJ--42一笔画问题【并查集】
- 程序员生存定律--升华成高手的可能方法
- 域名转让
- 使用dmidecode命令查看硬件信息
- Centrex业务-群内呼叫
- JSP过滤器(Filter)之实现全站解决中文提交乱码(包括get提交方式)
- 前端判断手机访问还是浏览器访问 chrome不支持
- NSSet 和 NSArray 区别
- B - Supermean
- 明升收藏
- map排序
- JDBC连接数据库
- Unity3d Log信息,发布版自动消失
- 算法笔记(四) 鸡尾酒排序