在国内项目中,经常会遇到页面传值为中文的情况,经常会遇到乱码。
现将我之前的一个过滤器放上来,供大家参考。(注:版权声明中的CottSoft.com为本人的项目,所以大家放心用。)
定义过滤器:
CharacterEncodingFilter.java
/*
**************************************************************************
* Confidentiality Information:
*
* This module is the confidential and proprietary information of
* CottSoft.com; it is not to be copied, reproduced, or
* transmitted in any form, by any means, in whole or in part,
* nor is it to be used for any purpose other than that for which
* it is expressly provided without the written permission of
* CottSoft.com.
*
**************************************************************************
*
* PROGRAM DESCRIPTION:
*
* This filter is processing Chinese code.
*
*
*
**************************************************************************
* CHANGE HISTORY:
*
* Date: by: Reason:
* YYYY-MM-DD IN Reason text.
*
* 2010-11-28 Simon Hoo Initial Version.
*************************************************************************
*/
package com.cottsoft.common.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 CharacterEncodingFilter implements Filter {
private String encoding = null;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding);
filterChain.doFilter(request, response);
}
public void destroy() {
// destroy code.
}
}
配制web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>cottsoft</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.cottsoft.common.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
分享到:
相关推荐
jsp传值中文乱码问题解决方法示例介绍.docx
jsp页面传值中文乱码问题处理.docx
用eclipse集成开发环境, jsp 过滤器——解决中文乱码问题
在jsp中,我们经常从数据库读取数据返回客户端,但我们常常在制作时出现乱码现象,下面与大家分享下jsp传值乱码解决办法,有类似情况的朋友可以参考下哈,希望对大家有所帮助
JSP中文问题解决办法,JSP页面传值,数据库等的中文乱码解决办法
利用过滤器解决JavaWeb(JSP)的乱码问题
配置Filter过滤器处理JSP中文乱码 参考Tomcat服务器目录webapps的examples示例 简单配置步骤: 1.在项目web.xml文件添加过滤器标记和; 2.实现过滤器代码; 3.对Tomcat服务器conf目录里的Server.xml文件配置...
jsp传值本地无乱码测试机出现乱码问题解决.docx
传值 乱码 处理传值 乱码 处理传值 乱码 处理传值 乱码 处理传值 乱码 处理传值 乱码 处理传值 乱码 处理传值 乱码 处理
在本地使用jsp传值时,一切正常无乱码,当放到测试机上是发现有乱码现象,针对此问题本文给出详细的解决方案,感兴趣的朋友可以参考下哈
jsp 课件 过滤器 中文乱码的处理
jsp用过滤器解决中文乱码问题的方法.docx
最近实验室做了一个项目,使用jspsmartupload来实现的文件的上传下载,原来在windows平台运行的挺好,但是后来系统移植到linux平台上,结果在上传或下载的文件名中有中文时就会出现乱码。后来查了网上一些人的解决...
JSP解决乱码JSP解决乱码JSP解决乱码JSP解决乱码JSP解决乱码
jspsmartupload中文乱码解决,内附struts1.2源码例子,jspsmartupload包及源代码
JSP页面上出现中文乱码的几个解决办法 比较好的 一般都能解决问题
JSP利用过滤器解决request中文乱码问题.docx
JSP用过滤器解决request getParameter中文乱码问题.docx