Posted on 2007-01-04 10:41
jmb's blog 阅读(2523)
评论(1) 编辑 收藏 引用
基本的编辑页面做好了,不过中文老是出现乱码,狂晕,怎么解决都不行,在网上查了很久,很多人都说通过下面这个方式解决:
response.setContentType("text/html;charset=GB2312");
request.setCharacterEncoding("GB2312");
不过,我用了,怎么也不行,老是出现错误,还是乱码,唉,看来还是不治本.
后来有个说通过修改../conf/Server.xml解决
<Connector port="9111" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />
不过还是乱码,看来还是不行....
后来,有人说通过Filter解决,从网上找了一个程序,唉,真的行啊,而且存入数据库里也是正确的,呵呵
java程序如下:
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 EncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Select and set (if needed) the character encoding to be used
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 {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
在网站webapp/WEB-INF/web.xml中添加:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>blog.util.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value></init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>