JSP传值中文乱码解决办法之——过滤器

在国内项目中,经常会遇到页面传值为中文的情况,经常会遇到乱码。

现将我之前的一个过滤器放上来,供大家参考。(注:版权声明中的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>

 

你可能感兴趣的:(java,Web,jsp,servlet,javaee)