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

首先看是從什麼地方開始出現的亂碼,只要統一編碼,就不會出現亂碼,下面以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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。