采用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 ...
随机推荐
- 全国出现大面积DNS服务器故障 域名被劫持
1月21日消息,继今日上午腾讯16项服务出现故障后,大量网站出现了无法访问的情况,据了解,该故障是由于国内DNS根服务器故障所致. 据了解,此次攻击式由于国内所有通用顶级域的根服务器出现异常,导致大量 ...
- Nginx随笔
1.用于代理与反代理,处理大量请求的工具. 2.主要有三大模块:handle.upstream.过滤模块.handle用于在nginx内部接到请求并进行处理的状况:upstream用于需要nginx接 ...
- svn,git,scp,rsync,rake,ssh,wget,axel,aria2,nohup,grep,tail,siege,mitmproxy,ulimit,netstat,lsof
scp 把本地文件上传到server上 scp -P 1234 config/cert/dev/client.pem dev@xx.xxx.xxx:/srv/rorapps/fgcc/config/c ...
- 题目:vbs批量开通工具,实现vbs开通的ux设计和流程调度
题目:vbs批量开通工具,实现vbs开通的ux设计和流程调度 需求点:支持开通前检查(检查失败不允许开站),开通过程监控,开通后业务检验,失败后重新开通,支持部分站点开通(比如用户导入的模板中有10个 ...
- Coding.net+Myeclipse 2014 Git配置
安装Git,然后 $ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # Creates a new ssh key, u ...
- Javascript中的数据类型知多少
JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定.这也意味着你可以使用同一个变量保存不同类型的数据 根据ECMAScript 5. ...
- web.config配置数据库连接(转)
摘自:http://www.cnblogs.com/breezeblew/archive/2008/05/01/1178719.html 第一种: 取连接字符串 string connString = ...
- HTML:调用静态页面html 的几种方法
今天做办公用品管理系统时,发现需要用到在一个静态页面html 中调用多个静态页面html的内容.查找资料总结了以下一些方法: 一.iframe引入的方法 代码如下: <!-- 部门--> ...
- WordPress网站搬家经验总结
http://cnzhx.net/blog/move-wordpress-site-step-by-step/也许很多人都有跟我类似的经历:因为某种原因需要将自己的WordPress站点从一个空间转移 ...
- C# partial 关键字详解
我们新建一个Windows Form时,后台代码都会自动添加如下代码: public partial class Form1 : Form { public Form1() { InitializeC ...