Home >php教程 >PHP开发 >Solution to the problem of Chinese garbled characters in mysql operated by jsp and servlet

Solution to the problem of Chinese garbled characters in mysql operated by jsp and servlet

高洛峰
高洛峰Original
2016-12-29 17:05:501344browse

First look at where the garbled characters start to appear. As long as the encoding is unified, there will be no garbled characters. The following takes uft-8 (personally think it is the best) as an example to explain in detail:

1. If The garbled code appears from the jsp page. Add the jsp header page:
e72754f50cb199268908504cce898271
Add the tag in the head tag.

2. If garbled characters appear in the servlet, there are two methods:
One is to add
request.setCharacterEncoding(" to the header of the doget and doPost methods in each servlet UTF-8″);
The second option is the safest, once and for all, it is to write a specially designed filter class, also known as internationalization. The class name is SetCharacterEncodingFilter and the content is as follows

package com.sharep.filter;//包名
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
{
 protected String encoding = null;
 protected FilterConfig filterConfig = null;
 protected boolean ignore = true;
 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
   this.ignore = false;
 }
 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 destroy()
 {
  this.encoding = null;
  this.filterConfig = null;
 }
 protected String selectEncoding(ServletRequest request)
 {
  return (this.encoding);
 }
}

Then in the web of web-inf Add the following code to .xml:

<filter>
  <filter-name>SetCharacterEncoding</filter-name>
  <filter-class>com.young.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>

This is done

3. If there are still garbled characters, it is a problem with the mysql database

1) Ensure that the database is established When the database encoding is selected, it is UTF-8. It is best to specify the encoding format in each table. MySQL default is latin1
2) If the MySQL version is 4.x or above, garbled characters still appear in the database. There are two types: Solution:
One is to specify the encoding method in the code to connect to the database:

String url = “jdbc:mysql://localhost:3306/test2?autoReconnect=true&useUnicode=true&characterEncoding=gbk&mysqlEncoding=utf8″ ;

If it still doesn’t work, use the

show variables like ‘collation_%&#39;;

command to check the default character set, if it is not utf If -8, change the corresponding encoding to utf8 in my.ini (windows) or my.cnf (linux) and then restart the mysql server and it will be ok

More solutions to the problem of mysql Chinese garbled characters in jsp and servlet operations For articles related to methods, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn