-------------------------------------
SetCharacterEncodingFilter.java
------------------------------------------------------------------------------
package com.core.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;
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.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 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 Methods
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
-----------------http://bbs.17testing.com/thread-6528-1-1.html---------------------------------------------------
在web.xml 添加:
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>
systemadmin.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
分享到:
相关推荐
关于StrutsPrepareAndExecuteFilter最新的过滤器,在Struts.xml定义常量<constant name="struts.i18n.encoding" value="UTF-8"/>无法解决post方式提交中文乱码问题。 关于Struts2各个版本、各种过滤器,用get方式...
character-encoding-filter 是 Java Web 乱码过滤器。 web.xml <filter-name>character-encoding-filter</filter-name> <filter-class>com.github.zhanhb.filter.CharacterEncodingFilter</filter-class> ...
乱码解决方案和过滤器的解决与用户的登陆error解决 乱码解决方案和过滤器的解决与用户的登陆error解决
STRUTS ActionForm乱码,servlet全局过滤器转义编码
struts2技巧,笔记.zip 指定404,500页面 utf-8过滤器 防SQL注入 解决乱码
NULL 博文链接:https://alexfc.iteye.com/blog/326497
JSP struts2 url传参中文乱码解决办法 1.设置struts.xml <constant name=struts.i18n....2.在web.xml添加编码过滤器 <filter>characterEncodingFilter <filter>org.springframework.web.filter.CharacterEncodingFil
中文乱码问题很头疼,但一个好的过滤器就可以解决问题啦
页面开发时经常会出现中文乱码问题,需要我们对编码进行转换。手工转换很麻烦,写个过滤器就好多了。
这是一个不错的例子,使用过滤器解决了字符乱码的问题
按照文件可以快速处理struts乱码问题,不须要写过滤器,非常好用
★ EncodingFilter 处理中文乱码的编码过滤器 ★ Action、Form、struts-config.xml web.xml struts的处理核心类及配置文件 ★ JSP调用页面(全面支持struts1.2)(增、删、改、查),分页功能自动实现 (如果数据库...
struts+eclipse+tomcate实现上传使用了过滤器解决了乱码问题。
Struts过滤器可以解决页面之间的乱码问题 一个类继承struts的RequestProcessor(分request 和response处理) 然后在struts-config.xml配置即可!
能够快速处理struts乱码问题,很使用,不要写过滤器和转码类
一个集成了分页和过滤器的struts小项目(学生成绩管理) ,方便的解决了分页和中文乱码的问题!!! 适合初学者!
Struts+Spring+Hibernate实现上传下载 本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本: •Struts 1.2 •Spring 1.2.5 •Hibernate 3.0 本文...
(4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 =================================...
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...