写一个filter
EncodingFilter
配置
URL为 /*
EncodingFilter
配置
URL为 /*
解决方案 »
- for循环中的if语句加break有什么后果!
- 求助
- 一个显示业务数据的问题!!
- 【Hibernate框架】Hibernate中Session的save(Object)方法是如何实现的?
- Resin 建2个站点,同时设为系统服务启动
- Struts上传中文文件名文件下载后变成乱码,怎么解决呀
- 下周有面试!高分求JAVA/J2EE / EJB / JMS 的电子书?
- 请教!struts中如何在DynactionForm 中初始化时间变量?
- XPATH中的冒号如何解决
- wildfly启动错误,尚且没有解决方案!
- JSF新手请教分页问题,先谢过各位了^_^
- DAO 和 Hibernate 的关系是怎样的?
还有页面的编码方式都改成utf-8.
还是写过滤器吧
1.insert进数据库的编码和数据库默认字符集不一致;
eg ,mysql4.1.11,mysql5默认为lain1,把myini中server,client的字符集该为default-character=GBK;(要加两次);
新建数据库时候把数据库默认字符集设为GBK
2.提交数据页面字符集和数据库字符集不一致;
这个google一下不过是转码;
我遇到的问题都是这样解决,不对地方别砖!!!!
package demo;import javax.servlet.*;
import java.io.IOException;
public class EncodeFilter 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 {
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);
}
}
配置文件:<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>demo.EncodeFilter </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>