JSP伝値中国語文字化けし解決方法の――フィルタ
国内プロジェクトでは、ページが中国語に転送されることが多く、文字化けしに遭遇することが多い.
私の前のフィルターを置いて、皆さんの参考にしてください.(注:著作権声明のCottSoft.comは本人のプロジェクトなので、安心して使ってください.)
フィルタの定義:
CharacterEncodingFilter.java
Web.xmlの作成
私の前のフィルターを置いて、皆さんの参考にしてください.(注:著作権声明の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>