happy
积分:60
发表主题:56
发表于 2009-06-23 17:28 资料
楼主
中文编码求助: 我在myeclipse的jsp页面上链接mysql,mysql的编码是GBK , jsp的也编码是gbk,但是读到JSP页面上是乱码,不知为何? 回复1: post方式提交数据,最好用servlet写个过滤器 request.setCharacterEncoding("gbk"); response.setCharacterEncoding("gbk"); 回复2: 看看IE的编码,在ie查看-》编码中查看,如果不是检查jsp页面,其实jsp页面有很多地方设置编码规则的,当然不同地方,控制地方也不同 回复3: 在后台输出一下,看看在MYEclipse中输出是不是有乱码! 回复4: 要是以上方法没有解决的话,建议用过滤器 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 SetCharacterEncodingFilter implements Filter { // ----------------------------------------------------- Instance Variables protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; // --------------------------------------------------------- Public Methods public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 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 { 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 Methods protected String selectEncoding(ServletRequest request) { return (this.encoding); } } 以下是web.xml配置 <filter> <filter-name>SetCharacterEncoding </filter-name> <filter-class>com.devil.struts.filter.SetCharacterEncodingFilter </filter-class> <init-param> <param-name>encoding </param-name> <param-value>UTF-8 </param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncoding </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>