Home> php教程> PHP开发> body text

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

高洛峰
Release: 2016-12-29 17:05:50
Original
1263 people have browsed it

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:
<%@ page language="java" pageEncoding="UTF-8" %>
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); } }
Copy after login

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

 SetCharacterEncoding com.young.filter.SetCharacterEncodingFilter//注意这里是类名,要有完整包名  encoding UTF-8    SetCharacterEncoding /* 
Copy after login

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″ ;
Copy after login

If it still doesn’t work, use the

show variables like ‘collation_%';
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!