<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>charset &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/charset/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 18 Apr 2023 08:16:56 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>charset &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>中文亂碼總整理 (Tomcat 5.5)</title>
		<link>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/</link>
					<comments>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 15:21:55 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[中文亂碼]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[JSP]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2902</guid>

					<description><![CDATA[<p>之前舊的部落格發過幾篇解決 Tomcat 中文亂碼問題的文章，確實是解決了一些中文顯示的問題，但今天在 Struts2 時，又再度遇到中文亂碼的問題，所以想把幾個可能忽略的步驟記錄下來。 其實解決中文&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/" data-wpel-link="internal">中文亂碼總整理 (Tomcat 5.5)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>之前舊的部落格發過幾篇解決 Tomcat 中文亂碼問題的文章，確實是解決了一些中文顯示的問題，但今天在 Struts2 時，又再度遇到中文亂碼的問題，所以想把幾個可能忽略的步驟記錄下來。</p>



<span id="more-2902"></span>



<p>其實解決中文亂碼，只要把所有的編碼格式都調整成一致，比如說，編輯器預設儲存編碼為 Big5，卻把頁面設定成 <code>&lt;%@ page contentType="text/html; charset=utf-8" %></code>，這樣一來，在瀏覽器開啟的網頁，中文就會是亂碼。(反過來也會是亂碼)。唯有兩者設定都是相同編碼的時候，在網頁上才能正確顯示中文。</p>



<p>上面提到，把所有的編碼都設成 Big5，這麼一來，也符合了「所有編碼都設為相同」的原則，當然包括編輯器預設儲存編碼也是 Big5，如此網頁可以正確顯示中文字，但是 &#8230; 如果後端需要加上 MySQL 資料庫，如何在表單跟 MySQL 之間正確傳遞中文，可能又會是一個花時間的問題，經過幾番折騰之後，決定把所有的編碼都設定為 utf-8 比較方便。</p>


<div id="rank-math-howto" class="rank-math-block" >
<div class="rank-math-howto-description">

</div>

<div class="rank-math-steps ">
<div id="howto-step-643e51f888560" class="rank-math-step">
<h3 class="rank-math-step-title ">編輯器預設儲存編碼 => 設定成 <strong>UTF-8</strong></h3>
<div class="rank-math-step-content "><p>以 EditPlus3 為例，可從「工具」->「偏好項」->「檔案」->「預設編碼模式」改成 UTF-8，<br />注意之前可能存成 Big5 的，可以按另存新檔後，最下面編碼模式可以改變。</p>
</div>
</div>
<div id="howto-step-643e51f888562" class="rank-math-step">
<h3 class="rank-math-step-title ">每個 JSP 頁面 charset 設定成 UTF-8</h3>
<div class="rank-math-step-content "><p>e.g. &lt;%@ page contentType=&#8221;text/html; charset=utf-8&#8243; %></p>
</div>
</div>
<div id="howto-step-643e51f888563" class="rank-math-step">
<h3 class="rank-math-step-title ">MySQL my.ini 設定成</h3>
<div class="rank-math-step-content ">[client]default-character-set=utf8<br />[mysqld]default-character-set=utf8</p>
</div>
</div>
<div id="howto-step-643e51f888564" class="rank-math-step">
<h3 class="rank-math-step-title ">資料庫、資料表及欄位的 <strong>character-set</strong> 設為 utf8，<strong>collation</strong>(校對)設定為utf8_unicode_ci。</h3>
<div class="rank-math-step-content "></div>
</div>
<div id="howto-step-643e51f888565" class="rank-math-step">
<h3 class="rank-math-step-title "><strong>資料庫連結部份</strong>修改</h3>
<div class="rank-math-step-content "><p>Connection con=DriverManager.getConnection(&#8220;jdbc:mysql://localhost/user?useUnicode=true&amp;characterEncoding=utf8&#8221;, &#8220;帳號&#8221;, &#8220;密碼&#8221;);</p>
</div>
</div>
<div id="howto-step-643e51f888566" class="rank-math-step">
<h3 class="rank-math-step-title ">Servlet 部份同樣把 <strong>charset</strong> 設定成 UTF-8</h3>
<div class="rank-math-step-content "><p>e.g. request.setContentType(&#8220;text/html; charset=utf-8&#8221;);</p>
</div>
</div>
<div id="howto-step-643e51f888567" class="rank-math-step">
<h3 class="rank-math-step-title ">GET 亂碼解決</h3>
<div class="rank-math-step-content "><p>在 %TOMCAT_HOME%\conf\server.xml 中，找到&lt;connector port=&#8221;8080&#8243; &#8230; 部分，加上 URIEncoding = &#8220;UTF-8&#8243;，重啟 Tomcat。</p>
</div>
</div>
<div id="howto-step-643e51f888568" class="rank-math-step">
<h3 class="rank-math-step-title ">POST 亂碼解決</h3>
<div class="rank-math-step-content "><p>若是有多個網站，將 %TOMCAT_HOME%\webapps\jsp-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class 複製到%TOMCAT_HOME%\shared\classes\filters 下，(若沒有資料夾可以自己新增)，修改 %TOMCAT_HOME%\webapps\(mytest)\WEB-INF\web.xml (mytest 為網站目錄)，加上以下內容 </p>
</div>
</div>
</div>
</div>


<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;filter> 
  &lt;filter-name>SetCharacterEncoding&lt;/filter-name> 
  &lt;filter-class>filters.SetCharacterEncodingFilter&lt;/filter-class> 
  &lt;init-param> 
    &lt;param-name>encoding&lt;/param-name> 
    &lt;param-value>Big5&lt;/param-value> 
  &lt;/init-param> 
&lt;/filter> 
&lt;filter-mapping> 
  &lt;filter-name>SetCharacterEncoding&lt;/filter-name> 
  &lt;url-pattern>/*&lt;/url-pattern> 
&lt;/filter-mapping></pre>



<p>照著這些步驟做，應該就能有個能正確顯示中文的平台(或許吧！至少小蛙現在的環境就是)如果發現什麼問題，再隨時補上。</p>



<p><strong>2009/07/31 補充：<br></strong>在使用 Prototype.js 的 Updater / Requester function 時，編碼為 big5 傳遞中文會發生亂碼，<br>PHP 的解決方式 -> <code>header("Content-Type:text/html;charset=BIG5");</code><br>JSP 解決方式 ->  <code>response.setHeader("Charset","BIG5"); </code><br>這裡 AJAX 的傳遞方式要用 get。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/" data-wpel-link="internal">中文亂碼總整理 (Tomcat 5.5)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
