解决页面中文的乱码问题,使用过滤器为所有页面配置编码格式,解决大部分的中文乱码问题
2017-08-13 00:18
851 查看
配置拦截器:
<!-- 为所有页面配置编码格式的过滤器 ,这里设置的编码格式需要与所有jsp页面的编码格式一致-->
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>(包路径).SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param> </filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
拦截器指向类:
/**
* Servlet乱码过滤器
* 只能过滤post请求的乱码问题.
*/
public class SetCharacterEncodingFilter implements Filter{
private String encoding = null;//编码集
private FilterConfig filterConfig = null;//配置信息
/**
* 初始化参数
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
//访问部署的描述文件(web.xml)中配置的过滤器初始化参数
this.encoding = filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//将请求转换为HttpServletRequest的请求
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
//System.out.println("serlvet编码过滤器");
if(encoding != null){
req.setCharacterEncoding(encoding);
resp.setCharacterEncoding(encoding);
}
//释放拦截器
chain.doFilter(req, resp);
}
/**
* 销毁
*/
@Override
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
}
<!-- 为所有页面配置编码格式的过滤器 ,这里设置的编码格式需要与所有jsp页面的编码格式一致-->
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>(包路径).SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param> </filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
拦截器指向类:
/**
* Servlet乱码过滤器
* 只能过滤post请求的乱码问题.
*/
public class SetCharacterEncodingFilter implements Filter{
private String encoding = null;//编码集
private FilterConfig filterConfig = null;//配置信息
/**
* 初始化参数
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
//访问部署的描述文件(web.xml)中配置的过滤器初始化参数
this.encoding = filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//将请求转换为HttpServletRequest的请求
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
//System.out.println("serlvet编码过滤器");
if(encoding != null){
req.setCharacterEncoding(encoding);
resp.setCharacterEncoding(encoding);
}
//释放拦截器
chain.doFilter(req, resp);
}
/**
* 销毁
*/
@Override
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
}
相关文章推荐
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
- 前台页面使用utf-8编码,url中包含中文参数时后台获取参数乱码问题解决
- eclipse下更改jsp页面默认的编码格式,解决中文乱码问题
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
- 设计能够导出所有记录到excel的分页(使用AspNetPager)页面,并解决excel文件名中文乱码问题
- Eclipse,tomcat及jsp页面编码的设定解决的中文乱码问题
- 使用过滤器解决中文乱码问题
- sl3中的DataGrid中的数据导出到Excel中 使用csv格式 解决中文是乱码的问题
- 关于Jsp页面乱码问题解决使用过滤器和包装类处理
- 使用过滤器解决中文编码问题
- JSP,mysql,tomcat下(基于struts2)中文及其乱码问题的解决 5大配置点 使用UTF-8编码
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题
- 使用过滤器Filter解决JSP+Servlet页面编码乱码
- JSP页面UTF-8格式中文字符串乱码问题解决方法
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题
- 黄聪:php传递URL中文编码为带%的编码获取后乱码问题(页面utf-8格式)
- 在JSP中使用过滤器解决中文乱码问题
- JSP页面编码问题研究三(中文字符乱码问题的解决方法)
- (转)XMLHTTP 乱码的解决方法(UTF8,GB2312 编码 解码)(附带解决DHTMLX不能用在GB2312页面编码下的中文问题)
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题