java struts 中文乱码处理
2008-04-12 13:41
330 查看
摘自: http://hi.baidu.com/rascal_hu/blog/item/e6dd2a342647b84a241f1470.html
1.写一个过滤器,完整代码如下:
package com.jesun.character.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;
/**
* @author Jesun
* @date 2008-2-16
* 源码来自Internet,我只作了简单的修改
* 解决Struts中文显示乱码问题
* */
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; //在web.xml文件里定义filter时要用到此参数
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// 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 {
// TODO Auto-generated method stub
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
//class end
(注:假如我们是用tomcat作服务器的话,那就更方便了,我们可以直接在tomcat的webapps\jsp-examples\Web-INF\classes\filters目录中的两个源文件RequestDumperFilter.java,SetCharacterEncodingFilter.java复制到你使用的包下。)
2.写好这个类以后,我们就在WEB应用的web.xml文件里定义Filter类,完整web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- 定义Filter,解决struts中文问题 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.jesun.character.Filter.SetCharacterEncodingFilter</filter-class>//包名
<init-param>
<!-- 定义编码格式,我用的是GBK -->
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<!-- innore参数是在过滤器类定义的 -->
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>*.jsp</servlet-name>
</filter-mapping>
<!-- Filter 定义结束 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/jesun.tld</taglib-uri>
<taglib-location>/WEB-INF/jesun.tld</taglib-location>
</taglib>
</web-app>
(3)以后编写JSP文件时,记得加入下面两句:
<%@page contentType="text/html;charset=GBK"%>
<META http-equiv=Content-Type content="text/html;charset=GBK">
到这里我们的工作就做完了,在程序里我们不需要再作任何其它处理,
1.写一个过滤器,完整代码如下:
package com.jesun.character.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;
/**
* @author Jesun
* @date 2008-2-16
* 源码来自Internet,我只作了简单的修改
* 解决Struts中文显示乱码问题
* */
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; //在web.xml文件里定义filter时要用到此参数
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// 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 {
// TODO Auto-generated method stub
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
//class end
(注:假如我们是用tomcat作服务器的话,那就更方便了,我们可以直接在tomcat的webapps\jsp-examples\Web-INF\classes\filters目录中的两个源文件RequestDumperFilter.java,SetCharacterEncodingFilter.java复制到你使用的包下。)
2.写好这个类以后,我们就在WEB应用的web.xml文件里定义Filter类,完整web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- 定义Filter,解决struts中文问题 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.jesun.character.Filter.SetCharacterEncodingFilter</filter-class>//包名
<init-param>
<!-- 定义编码格式,我用的是GBK -->
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<!-- innore参数是在过滤器类定义的 -->
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>*.jsp</servlet-name>
</filter-mapping>
<!-- Filter 定义结束 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/jesun.tld</taglib-uri>
<taglib-location>/WEB-INF/jesun.tld</taglib-location>
</taglib>
</web-app>
(3)以后编写JSP文件时,记得加入下面两句:
<%@page contentType="text/html;charset=GBK"%>
<META http-equiv=Content-Type content="text/html;charset=GBK">
到这里我们的工作就做完了,在程序里我们不需要再作任何其它处理,
相关文章推荐
- Struts中使处理中文乱码问题-----Filter
- 处理struts开发中的中文乱码问题
- JAVA 中URL链接中文参数乱码的若干处理方法
- javaWed项目中用过滤器实现转码功能,敏感词汇过滤更能,处理Get和Post接收数据中的中文乱码问题以及敏感词汇的处理
- java环境url中文参数乱码处理
- JAVA——URL链接中文参数乱码的若干处理方法
- Java连接MySQL中文乱码处理
- struts处理中文乱码问题总结
- java判断中文编码而后处理中文编码乱码问题
- JAVA中URL链接中文参数乱码处理方法
- javaWeb-Servlet的继承体系和HttpServletRequest中常用的方法+HttpServletResponse对中文乱码的处理
- struts处理中文乱码问题总结
- struts1.2中处理中文乱码问题
- java web应用文件下载(包括中文文件名乱码处理)
- JAVA 中URL链接中文参数乱码的处理方法
- struts处理中文乱码问题总结
- JAVA 中URL中文参数乱码的处理方法
- Java插入MySQL中文乱码,简单的处理方法
- JAVA 中URL链接中文参数乱码的处理方法
- struts处理中文乱码问题总结