SetCharacterEncodingFilter.java的代码:
package com.classroom.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;
import javax.servlet.UnavailableException;public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null;
this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
System.out.println("SetCharacterEncodingFilter Execute!!!!!!");
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response); }
public void init(FilterConfig filterConfig) throws ServletException { 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);
}
}
WEB.XML中关于FILTER的设置是:
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.classroom.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
package com.classroom.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;
import javax.servlet.UnavailableException;public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null;
this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
System.out.println("SetCharacterEncodingFilter Execute!!!!!!");
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response); }
public void init(FilterConfig filterConfig) throws ServletException { 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);
}
}
WEB.XML中关于FILTER的设置是:
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.classroom.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
解决方案 »
- 为什么JSP里的<form>标签里写的onsubmit="return pwderr()"会报错?
- 求DAT音频格式转化为WMV格式的代码java
- 急急急急急急,在修改数据后无法加到数据库中去,显示如下错误。在线等高手解答
- 关于james
- tomcat5.0.28连接mysql数据源出错。
- 为什么java -version过不了
- session的怪问题,痛哭中......
- 请教各位,怎样把gb2312的字符串解析出来? 比如: =?gb2312?B?s6PTw9PKvP6x4MLrvLDC0sLrtcS94r72MjAwMcTqMDTUwjI1yNUgMA==?=
- 询问一个关于EJB-SELECT的例子,请给于帮助~~`~
- 有关Webservice中Endpoint.publish的问题
- rwebwork2+FreeMarker 制作数据列表显示简单实例
- 如何在java中使用package传出的数组?
用户访问服务器,struts根据用户浏览器的语言来寻找对应的资源文件,如果找不到这种语言的资源文件,将使用默认的资源文件(在struts-config.xml中指定的那个)。
另外,所有非英语的资源文件都需要进行native2ascii转码,否则出现上边说的问号问题。
举例:
比如在struts-config.xml中已经指定了如下的资源文件
message-resources parameter="com.unida.egov.mwp.struts.ApplicationResources"
也就是ApplicationResources.properties。
这个文件中应该全是英文,当找不到用户需要的语言的资源文件时,将统一使用这个文件来输出内容。
现在我们来为中文配置一个资源文件,新建一个temp.properties文件,然后将ApplicationResources.properties中的英文内容拷入其中。现在把temp.properties中的英文全部用中文替换。
现在用native2ascii temp.properties ApplicationResources_zh_CN.properties命令,生成一个转码后的中文资源文件。
如果需要其他语言,方法也是类似的,只是要在ApplicationResources_后边加上国家和语言的编码。
到这里,看看我们有哪些东西了,一个ApplicationResources.properties(默认文件:英语)、ApplicationResources_zh_CN.properties(中文文件:中文)
那个temp.properties只是一个临时文件,实际不需要。
把这两个文件放在一起,而struts-config.xml文件中只需要指出它们所在的位置即可,如前。
还需要做的就是,所有jsp页面的编码都应该是UTF-8,page language="java" contentType="text/html;charset=UTF-8"现在,当用户访问服务器时,根据用户的语言来选择资源文件,找不到的时候就用默认的文件(英语),这样就实现了struts的国际化支持了。如果使用eclipse开发,有一个叫做JInto的插件,可以在同一个编辑器中对应着编辑所有的资源文件并自动完成转码的工作,非常方便。
等下我发中文显示的。
最后就是中间数据传递的编码转换问题了,你可以使用Spring提供的一个过滤器,不用自己来写。在web.xml中加入
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>如此配置后,即可实现全面的国际化了。这是我在实际项目中的开发方式,应用部署到windows或linux平台都不用做任何修改即可正常使用。推荐大家都采用这种方式进行国际化或中文显示的处理,哦,不单是中文了,各种语言都可以正常处理了。
response.setContentType(“text/html;charset=GBK”);、、、、或使用FilterJsp中
s<%@ page contentType="text/html;charset=GBK"%>Html中
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK">
</head>再试试好使了么?
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;public class EncodingFilter implements Filter {
protected FilterConfig filterConfig;
private String targetEncoding = "gb2312";
public void init(FilterConfig arg0) throws ServletException { } public void doFilter(
ServletRequest srequest,
ServletResponse sresponse,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)srequest;
request.setCharacterEncoding(targetEncoding);
chain.doFilter(srequest,sresponse);
}
public void destroy() {
}}在你的web.xml文件中设置:
<filter>
<filter-name>encodding</filter-name>
<filter-class>com.common.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>在每个页上都加上:
<%@ page contentType="text/html;charset=gb2312" %>
……<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
……
</head>我把自己用的贴上供参考,效果不错。
try{ request.setCharacterEncoding(encoding);}catch(Exception e) {}
就可以了