首頁 php教程 PHP开发 jsp和servlet操作mysql中文亂碼問題的解決辦法

jsp和servlet操作mysql中文亂碼問題的解決辦法

Dec 29, 2016 pm 05:05 PM

首先看是從什麼地方開始出現的亂碼,只要統一編碼,就不會出現亂碼,下面以uft-8(個人認為最好)為例,詳細說明:

1、如果亂碼是從jsp頁面出現的,jsp頭部頁加上:

在head標籤中加上標籤。

2、如果亂碼是在servlet中出現的,則有兩種方法:
一種是在每個servlet中doget和doPost方法頭部加上
request.setCharacterEncoding(“UTF-8″);
第二種最保險,一勞永逸,是專門寫一個過濾器類,也稱國際化,類名為SetCharacterEncodingFilter內容如下

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);
 }
}
登入後複製

然後在web-inf的web.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>
登入後複製

這樣就搞定了

3、如果還是有亂碼,就是mysql資料庫的問題了

1)保證建立資料庫的時候資料庫編碼選擇的是utf-8,最好在每個表中也指定編碼格式,mysql預設是latin1
2)如果mysql版本是4.x以上,資料庫中還是出現亂碼,有以下兩種解決方法:
一種是在連接資料庫的程式碼中指定編碼方式:

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

如果還是不行的話就是用

show variables like ‘collation_%&#39;;
登入後複製

這個指令來查看預設字元集,如果不是utf-8的話在my.ini(windows)或是my.cnf(linux)將對應的編碼修改成utf8之後重啟mysql伺服器就ok了

更多jsp和servlet操作mysql中文亂碼問題的解決方法相關文章請關注PHP中文網!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)