<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
MySQL
Here is an doc for MySQL and Java io: http://blog.csdn.net/liuxueyongyu/article/details/2026788
Server encoding: iso-8859-1
Set page encoding to show Chinese character
response.setContentType("text/html;charset=utf-8");
Data request
- form post
- form get
- url from html
- sendRedirect
1. form post
Browser: utf-8
Web Server: iso-8859-1
Solution
request.setCharacterEncoding("utf-8")
2. form get / 3. url from html
Browser: utf-8
Web Server: iso-8859-1
Solution 1
@WebFilter("/CharacterEncoding") public class CharacterEncoding implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); //set request character encoding chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
In web.xml
Write a filter by yourself
<filter> <filter-name>CharacterEncoding</filter-name> <filter-class>com.gvace.notebook.web.filter.CharacterEncoding</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
or Spring Web Filter(do not need to write a class then)
<!-- Encoding with spring filter --> <filter> <filter-name>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> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Solution 2
request.setCharacterEncoding("utf-8")
Solution 3
Solution 3
new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
4. sendRedirect or All strings in URL
String info= java.net.URLEncoder.encode(name,"utf-8");
response.sendRedirect("EncodingPro/Welcome?username="+info);
Data Response
response.setContentType("text/html;charset=utf-8");
Cookie with Different Character set
Saving
String val = java.net.URLEncoder.encode("中国字","utf-8"); Cookie cookie = new Cookie("name",val);Getting
String val = java.net.URLDecoder.decode(cookie.getValue(),"utf-8");
Properties file
For character encoding in properties file
Use native2ascii from jdk/bin/, copy the String into native2ascii, it will translate to ascii
No comments:
Post a Comment