项目字符集总结

xiaoxiao2022-08-12  95

robbin 写道 MySQL数据库的4.1是一个分水岭,4.1直接支持Unicode,以下版本支持的不好; MySQL JDBC Driver的3.0.16也是一个分水岭,3.0.16版本会取数据库本身的编码,然后按照该编码转换,这种方式和Oracle的JDBC Driver是一样的。例如你的数据库是GBK编码的话,JDBC Driver就会把数据库里面的取出来的字符串按照GBK往unicode转换,送给JVM。因此正确的设置数据库本身的编码就尤为重要。 MySQL JDBC Driver3.0.16以下的版本则不然,它不会那么智能的根据数据库编码来确定如何转换,它总是默认使用ISO8859-1,因此你必须使用 characterEncoding=GBK来强制他把数据库中取出来的字符串按照GBK来往unicode转换。 因此,使用什么数据库版本,不管是3.x,还是4.0.x还是4.1.x,其实对我们来说不重要,重要的有二: 1) 正确的设定数据库编码,MySQL4.0以下版本的字符集总是默认ISO8859-1,MySQL4.1在安装的时候会让你选择。如果你准备使用UTF- 8,那么在创建数据库的时候就要指定好UTF-8(创建好以后也可以改,4.1以上版本还可以单独指定表的字符集) 2) 使用3.0.16以上版本的JDBC Driver,那么你就不需要再写什么characterEncoding=UTF-8

  根据robbin的提示,项目中关于字符集问题的基本解决方案

 

所用环境是struts2+ spring 2.5+hibernate 3.2 . DB用mysql 5.0

 

数据库的字符集 mysql4.1 之后由于可以设定表的字符集,于是需要注意的就是在创建表的时候设置字符集。 创建表的时候,我们添加如下代码character set =utf8; hibernate读写字符集 因为这里用spring+hibernate,所以在设置dataSource bean的时候进行设计就可以了<property name="url" value="jdbc:mysql://localhost:3306/100see?useUnicode=true&amp;characterEncoding=utf8" />  添加web.xml过滤器 过滤器定义 写道 Servlet过滤器是用户请求和处理程序之间的一层处理程序,能够对与过滤器关联的URL请求和响应进行检查和修改。Servlet过滤器能够在 Servlet被调用之前检查Request对象,修改Request Header和Request内容;在Servlet被调用之后检查Response对象,修改Response Header和Response内容。Servlet过滤器过滤的URL资源可以是Servlet、JSP、HTML文件,或者是整个路径下的任何资源。多个过滤器可以构成一个过滤器链,当请求与过滤器关联的URL的时候,过滤器链上的过滤器就会挨个发生作用,除非某个过滤器终止了这个过程。  由于不同项目的字符集设置有可能导致字符集混乱,因此我们通过设置过滤器,完成servlet之间的字符集转换问题 很多时候我们都会用到spring的过滤器<filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>  但是我们发现spring的过滤器只对request做字符集的过滤。因此建议实现自己的过滤器package com.starweb.core.util; 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 EncodingFilter implements Filter { /** * The default character encoding to set for requests that pass through this * filter. */ protected String encoding = null; /** * The filter configuration object we are associated with. If this value is * null, this filter instance is not currently configured. */ protected FilterConfig filterConfig = null; /** * Should a character encoding specified by the client be ignored? */ protected boolean ignore = true; // --------------------------------------------------------- Public Methods /** * Take this filter out of service. */ public void destroy() { this.encoding = null; this.filterConfig = null; } /** * Select and set (if specified) the character encoding to be used to * intERPret request parameters for this request. * * @param request * The Servlet request we are processing * @param result * The servlet response we are creating * @param chain * The filter chain we are processing * * @exception IOException * if an input/output error occurs * @exception ServletException * if a servlet error occurs */ 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); response.setContentType("text/html;charset="+encoding+"\""); } // System.out.println("requestEncoding : " + request.getCharacterEncoding()); // System.out.println("responseEncoding : " + response.getCharacterEncoding()); } // Pass control on to the next filter chain.doFilter(request, response); } /** * Place this filter into service. * * @param filterConfig * The filter configuration object */ 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 /** * Select an appropriate character encoding to be used, based on the * characteristics of the current request and/or filter initialization * parameters. If no character encoding should be set, return * <code>null</code>. * <p> * The default implementation unconditionally returns the value configured * by the <strong>encoding</strong> initialization parameter for this * filter. * * @param request * The servlet request we are processing */ protected String selectEncoding(ServletRequest request) { return (this.encoding); } }   jsp字符集问题 jsp页面设置UTF-8<%@ page language="java" pageEncoding="UTF-8"%>  struts2字符集设置 struts2 可以设置字符集。默认情况下iso-8859-1字符集,  设置struts2.xml<constant name="struts.i18n.encoding" value="UTF-8" />  ps: struts2的国际化有时候失败,自己用filter控制字符集问题比较好

 

转载请注明原文地址: https://www.6miu.com/read-4974498.html

最新回复(0)