采用Filter的方法解决Servlet的编码问题
这样比你自己在Servlet代码中硬编码request.setCharacterEncoding, response.setCharacterEncoding方便多了
总之,如果你添加了这个filter,配置好了web.xml,那么如果还出现乱码问题,你就去检查你的JSP和HTML代码中的encoding选项吧(charset, pageEncoding, meta.content之类的),看看是否和你在web.xml中配置的filter的encoding相匹配
CharacterEncodingFilter.java
public class CharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean enable = false;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (this.enable) {
String encoding = selectEncoding(request);
if (encoding != null && !encoding.equals("")) {
System.out.println(this + ": ignore = true " + encoding);
request.setCharacterEncoding(encoding); //Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
response.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
//
String enable = filterConfig.getInitParameter("enable");
if (enable.equalsIgnoreCase("true")) {
this.enable = true;
} else {
this.enable = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml
<filter>
<filter-name>charset-encoding</filter-name>
<filter-class>cn.mldn.lxh.encoding.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>charset-encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
采用Filter的方法解决Servlet的编码问题的更多相关文章
- 采用Filter的方法解决HttpServletRequest.getParameter乱码的问题
其实就是利用这么一个原理: byte[] bytes = str.getBytes("iso-8859-1"); String result = new String(bytes, ...
- servlet请求编码与响应编码问题(编码不一致可能会导致乱码)
html中的编码 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&g ...
- JavaWeb之动态代理解决request请求编码问题
动态代理解决编码问题 1.设计模式 出现原因:软件开发过程中,遇到相似问题,将问题的解决方法抽取模型(套路) 常见设计模式:单例,工厂,适配器,装饰者,动态代理. 2.装饰者模式简单介绍 谷歌汽车开发 ...
- 【JAVAWEB学习笔记】24_filter实现自动登录和解决全局的编码问题
过滤器Filter 学习目标 案例-自动登录 案例-解决全局的编码 一.过滤器Filter 1.filter的简介 filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标 ...
- 学习笔记:Maven构造版本号的方法解决浏览器缓存问题
需要解决的问题 在做WEB系统开发时,为了提高性能会利用浏览器的缓存功能,其实即使不显式的申明缓存,现代的浏览器都会对静态文件(js.css.图片之类)缓存.但也正因为这个问题导致一个问题,就是资源的 ...
- CTF-练习平台-Misc之 多种方法解决
五.多种方法解决 题目提示:在做题过程中你会得到一个二维码图片 下载文件后解压发现是一个exe文件,打开后报错:将文件后缀名改为txt打开后发现是base64编码 联系到提示说最后是一个二维码,将它转 ...
- web.xml中Filter,Listener,Servlet的区别
一.Servlet Servlet是基本的服务端程序,他来自接口Servlet,接口中有方法service.而Servlet的一个重要实现类,则是tomcat服务器的核心,那就是HttpServlet ...
- 可采用两种方法得到一个EJB对象
(本文是转载其他人的技术文章,觉得说得挺浅显易懂,特借来学习) 在前面学习的例子中,只有一个EJB,但是对于一个真实的项目,EJB的数量可以有很多,而且EJB之间也会互相调用,那么在一个EJB ...
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果
一直很纠结,Oracle的快速返回机制,虽然结果集很多,可是它能很快的显示第一个结果,虽然通过MYSQl的客户端可以做到,但是通过JDBC却不行. 今天用了1个多小时,终于搞定此问题,希望对广大Jav ...
随机推荐
- JAVA中定义常量的几种方式
1.最古老的 //未处理 public static final Integer PROCESS_STATUS_UNTREATED = 0; //已接收 public static final Int ...
- IDEA基于maven整合SSM
感谢:IDEA搭建Spring+SpringMVC+mybatis框架教程 简洁明了, 步骤详细.
- 基于JavaScript 声明全局变量的三种方式
本文转自脚本之家:http://www.jb51.net/article/36548.htm JS中声明全局变量主要分为显式声明或者隐式声明下面分别介绍. 声明方式一: 使用var(关键字)+变量名( ...
- HDUOJ--1874 畅通工程续
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- DPDK架构与特点
当年在某公司实习的时候,当时老大给了我一份DPDK的文档,说是将来很有用,熟悉DPDK对能力提高也有帮助,就试着翻译了 <Intel DPDK Getting Started Guide> ...
- PLSQL_统计信息系列08_统计信息生成和还原
2015-02-01 Created By BaoXinjian
- Oschina 安卓client源代码学习之中的一个
今天主要研究一下两个功能 (1)双击返回键退出程序 (2)接近完美地退出程序 (1) 在非常多应用程序里都有一个功能,就是点击返回键,之后提示你再点击返回键就退出程序. 之前一直非常好奇这是怎么实现的 ...
- 读"U盘小偷"有感
作者: sudami 嘿嘿,今天终于有时间学习自己喜欢的东西了,在kanxue里看到一篇关于U盘小偷的文章:http://bbs.pediy.com/showthread.php?p=381656#p ...
- app hybrid
package com.note.testcases; /** * * The MIT License (MIT) * * Copyright (c) 2016 Alejandro Gómez Mor ...