tomcat_charset.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?xml version="1.0" encoding="EUC-JP"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  4. <head><script src="//archive.org/includes/analytics.js?v=cf34f82" type="text/javascript"></script>
  5. <script type="text/javascript">window.addEventListener('DOMContentLoaded',function(){var v=archive_analytics.values;v.service='wb';v.server_name='wwwb-app224.us.archive.org';v.server_ms=381;archive_analytics.send_pageview({});});</script>
  6. <script type="text/javascript" src="/_static/js/bundle-playback.js?v=poeZ53Bz" charset="utf-8"></script>
  7. <script type="text/javascript" src="/_static/js/wombat.js?v=UHAOicsW" charset="utf-8"></script>
  8. <script type="text/javascript">
  9. __wm.init("https://web.archive.org/web");
  10. __wm.wombat("http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html","20181003202907","https://web.archive.org/","web","/_static/",
  11. "1538598547");
  12. </script>
  13. <link rel="stylesheet" type="text/css" href="/_static/css/banner-styles.css?v=fantwOh2" />
  14. <link rel="stylesheet" type="text/css" href="/_static/css/iconochive.css?v=qtvMKcIJ" />
  15. <!-- End Wayback Rewrite JS Include -->
  16. <meta http-equiv="content-type" content="text/html"/>
  17. <meta http-equiv="content-style-type" content="text/css"/>
  18. <title>TomcatでUTF-8/EUCを使う</title>
  19. <link rel="stylesheet" type="text/css" href="/web/20181003202907cs_/http://www.nina.jp/html.css"/>
  20. <link rel="shortcut icon" href="https://web.archive.org/web/20181003202907im_/http://www.nina.jp/img/nina.ico"/>
  21. </head>
  22. <body><!-- BEGIN WAYBACK TOOLBAR INSERT -->
  23. <style type="text/css">
  24. body {
  25. margin-top:0 !important;
  26. padding-top:0 !important;
  27. /*min-width:800px !important;*/
  28. }
  29. </style>
  30. <script>__wm.rw(0);</script>
  31. <div id="wm-ipp-base" lang="en" style="display:none;direction:ltr;">
  32. <div id="wm-ipp" style="position:fixed;left:0;top:0;right:0;">
  33. <div id="donato" style="position:relative;width:100%;">
  34. <div id="donato-base">
  35. <iframe id="donato-if" src="https://archive.org/includes/donate.php?as_page=1&amp;platform=wb&amp;referer=https%3A//web.archive.org/web/20181003202907/http%3A//www.nina.jp/server/slackware/webapp/tomcat_charset.html"
  36. scrolling="no" frameborder="0" style="width:100%; height:100%">
  37. </iframe>
  38. </div>
  39. </div><div id="wm-ipp-inside">
  40. <div id="wm-toolbar" style="position:relative;display:flex;flex-flow:row nowrap;justify-content:space-between;">
  41. <div id="wm-logo" style="/*width:110px;*/padding-top:12px;">
  42. <a href="/web/" title="Wayback Machine home page"><img src="/_static/images/toolbar/wayback-toolbar-logo-200.png" srcset="/_static/images/toolbar/wayback-toolbar-logo-100.png, /_static/images/toolbar/wayback-toolbar-logo-150.png 1.5x, /_static/images/toolbar/wayback-toolbar-logo-200.png 2x" alt="Wayback Machine" style="width:100px" border="0" /></a>
  43. </div>
  44. <div class="c" style="display:flex;flex-flow:column nowrap;justify-content:space-between;flex:1;">
  45. <form class="u" style="display:flex;flex-direction:row;flex-wrap:nowrap;" target="_top" method="get" action="/web/submit" name="wmtb" id="wmtb"><input type="text" name="url" id="wmtbURL" value="http://www.nina.jp/server/slackware/webapp/tomcat_charset.html" onfocus="this.focus();this.select();" style="flex:1;"/><input type="hidden" name="type" value="replay" /><input type="hidden" name="date" value="20181003202907" /><input type="submit" value="Go" />
  46. </form>
  47. <div style="display:flex;flex-flow:row nowrap;align-items:flex-end;">
  48. <div class="s" id="wm-nav-captures">
  49. <a class="t" href="/web/20181003202907*/http://www.nina.jp/server/slackware/webapp/tomcat_charset.html" title="See a list of every capture for this URL">5 captures</a>
  50. <div class="r" title="Timespan for captures of this URL">10 Jan 2012 - 03 Oct 2018</div>
  51. </div>
  52. <div class="k" style="flex:1;">
  53. <a href="" id="wm-graph-anchor">
  54. <div id="wm-ipp-sparkline" title="Explore captures for this URL" style="position: relative">
  55. <canvas id="wm-sparkline-canvas" width="675" height="27" border="0"></canvas>
  56. </div>
  57. </a>
  58. </div>
  59. </div>
  60. </div>
  61. <div class="n">
  62. <table>
  63. <tbody>
  64. <!-- NEXT/PREV MONTH NAV AND MONTH INDICATOR -->
  65. <tr class="m">
  66. <td class="b" nowrap="nowrap"><a href="https://web.archive.org/web/20160912162501/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html" title="12 Sep 2016"><strong>Sep</strong></a></td>
  67. <td class="c" id="displayMonthEl" title="You are here: 20:29:07 Oct 03, 2018">OCT</td>
  68. <td class="f" nowrap="nowrap">Nov</td>
  69. </tr>
  70. <!-- NEXT/PREV CAPTURE NAV AND DAY OF MONTH INDICATOR -->
  71. <tr class="d">
  72. <td class="b" nowrap="nowrap"><a href="https://web.archive.org/web/20160912162501/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html" title="16:25:01 Sep 12, 2016"><img src="/_static/images/toolbar/wm_tb_prv_on.png" alt="Previous capture" width="14" height="16" border="0" /></a></td>
  73. <td class="c" id="displayDayEl" style="width:34px;font-size:22px;white-space:nowrap;" title="You are here: 20:29:07 Oct 03, 2018">03</td>
  74. <td class="f" nowrap="nowrap"><img src="/_static/images/toolbar/wm_tb_nxt_off.png" alt="Next capture" width="14" height="16" border="0" /></td>
  75. </tr>
  76. <!-- NEXT/PREV YEAR NAV AND YEAR INDICATOR -->
  77. <tr class="y">
  78. <td class="b" nowrap="nowrap"><a href="https://web.archive.org/web/20160912162501/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html" title="12 Sep 2016"><strong>2016</strong></a></td>
  79. <td class="c" id="displayYearEl" title="You are here: 20:29:07 Oct 03, 2018">2018</td>
  80. <td class="f" nowrap="nowrap">2019</td>
  81. </tr>
  82. </tbody>
  83. </table>
  84. </div>
  85. <div class="r" style="display:flex;flex-flow:column nowrap;align-items:flex-end;justify-content:space-between;">
  86. <div id="wm-btns" style="text-align:right;height:23px;">
  87. <span class="xxs">
  88. <div id="wm-save-snapshot-success">success</div>
  89. <div id="wm-save-snapshot-fail">fail</div>
  90. <a id="wm-save-snapshot-open" href="#"
  91. title="Share via My Web Archive" >
  92. <span class="iconochive-web"></span>
  93. </a>
  94. <a href="https://archive.org/account/login.php" title="Sign In" id="wm-sign-in">
  95. <span class="iconochive-person"></span>
  96. </a>
  97. <span id="wm-save-snapshot-in-progress" class="iconochive-web"></span>
  98. </span>
  99. <a class="xxs" href="http://faq.web.archive.org/" title="Get some help using the Wayback Machine" style="top:-6px;"><span class="iconochive-question" style="color:rgb(87,186,244);font-size:160%;"></span></a>
  100. <a id="wm-tb-close" href="#close" style="top:-2px;" title="Close the toolbar"><span class="iconochive-remove-circle" style="color:#888888;font-size:240%;"></span></a>
  101. </div>
  102. <div id="wm-share" class="xxs">
  103. <a href="/web/20181003202907/http://web.archive.org/screenshot/http://www.nina.jp/server/slackware/webapp/tomcat_charset.html"
  104. id="wm-screenshot"
  105. title="screenshot">
  106. <span class="wm-icon-screen-shot"></span>
  107. </a>
  108. <a href="#" id="wm-video" title="video">
  109. <span class="iconochive-movies"></span>
  110. </a>
  111. <a id="wm-share-facebook" href="#" data-url="https://web.archive.org/web/20181003202907/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html" title="Share on Facebook" style="margin-right:5px;" target="_blank"><span class="iconochive-facebook" style="color:#3b5998;font-size:160%;"></span></a>
  112. <a id="wm-share-twitter" href="#" data-url="https://web.archive.org/web/20181003202907/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html" title="Share on Twitter" style="margin-right:5px;" target="_blank"><span class="iconochive-twitter" style="color:#1dcaff;font-size:160%;"></span></a>
  113. </div>
  114. <div style="padding-right:2px;text-align:right;white-space:nowrap;">
  115. <a id="wm-expand" class="wm-btn wm-closed" href="#expand" onclick="__wm.ex(event);return false;"><span id="wm-expand-icon" class="iconochive-down-solid"></span> <span class="xxs" style="font-size:80%;">About this capture</span></a>
  116. </div>
  117. </div>
  118. </div>
  119. <div id="wm-capinfo" style="border-top:1px solid #777;display:none; overflow: hidden">
  120. <div id="wm-capinfo-collected-by">
  121. <div style="background-color:#666;color:#fff;font-weight:bold;text-align:center">COLLECTED BY</div>
  122. <div style="padding:3px;position:relative" id="wm-collected-by-content">
  123. <div style="display:inline-block;vertical-align:top;width:50%;">
  124. <span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls);"></span>
  125. Organization: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a>
  126. <div style="max-height:75px;overflow:hidden;position:relative;">
  127. <div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
  128. Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
  129. </div>
  130. </div>
  131. <div style="display:inline-block;vertical-align:top;width:49%;">
  132. <span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls)"></span>
  133. <div>Collection: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a></div>
  134. <div style="max-height:75px;overflow:hidden;position:relative;">
  135. <div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
  136. Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. <div id="wm-capinfo-timestamps">
  142. <div style="background-color:#666;color:#fff;font-weight:bold;text-align:center" title="Timestamps for the elements of this page">TIMESTAMPS</div>
  143. <div>
  144. <div id="wm-capresources" style="margin:0 5px 5px 5px;max-height:250px;overflow-y:scroll !important"></div>
  145. <div id="wm-capresources-loading" style="text-align:left;margin:0 20px 5px 5px;display:none"><img src="/_static/images/loading.gif" alt="loading" /></div>
  146. </div>
  147. </div>
  148. </div></div></div></div><div id="wm-ipp-print">The Wayback Machine - https://web.archive.org/web/20181003202907/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html</div>
  149. <script type="text/javascript">
  150. __wm.bt(675,27,25,2,"web","http://www.nina.jp/server/slackware/webapp/tomcat_charset.html","20181003202907",1996,"/_static/",["/_static/css/banner-styles.css?v=fantwOh2","/_static/css/iconochive.css?v=qtvMKcIJ"], false);
  151. __wm.rw(1);
  152. </script>
  153. <!-- END WAYBACK TOOLBAR INSERT -->
  154. <p class="dig1"><span class="title1">TomcatでUTF-8/EUC-JPを使う</span></p>
  155. <p class="dig1">
  156. [<a href="/web/20181003202907/http://www.nina.jp/server/index-slackware.html">サーバの実験室 Slackware</a>]
  157. </p>
  158. <p class="right">
  159. 作成 : 2004/12/31
  160. </p>
  161. <form action="/web/20181003202907/http://www.nina.jp/namazu/namazu.cgi" method="get">
  162. <p class="right">
  163. "サーバの実験室"の検索
  164. <input type="text" name="query" size="25" value=""/>
  165. <input type="submit" value="検索"/>
  166. <input type="reset" value="クリア"/>
  167. </p>
  168. </form>
  169. <hr/>
  170. <p class="dig2">
  171. Tomcatから返されるキャラクタセットの情報は、<span class="bold">httpd.conf</span>のルートで指定したAddDefaultCharsetの値と同じになるらしい。
  172. Directoryディレクティブの中で指定したAddDefaultCharsetは無視されるっぽい。
  173. ついでに、metaタグも無視されるみたい。
  174. <span class="blue">(&lt;---このへん、後述のSetCharacterEncodingFilterがちゃんと動作しないこともあり、自信なし...)</span>
  175. </p>
  176. <p class="dig2">
  177. うちのWEBサーバはルートのAddDefaultCharsetでEUC-JP指定をしており、コンテキストパス以下はUTF-8にしたいので、なんらかの対策をしないと文字化けしてしまう。
  178. </p>
  179. <p class="dig1"><span class="title2">サーブレットの場合</span></p>
  180. <p class="dig2">
  181. <span class="bold">response.setContentType</span>でキャラクタセットを指定する。
  182. EUC-JPを使用するなら、response.setContentType("text/html; <span class="bold">charset=EUC-JP</span>")。
  183. UTF-8を使用するなら、response.setContentType("text/html; <span class="bold">charset=UTF-8</span>")。
  184. </p>
  185. <pre class="dig3 text">
  186. # HelloWorld.java
  187. import java.io.*;
  188. import java.text.*;
  189. import java.util.*;
  190. import javax.servlet.*;
  191. import javax.servlet.http.*;
  192. public class HelloWorld extends HttpServlet {
  193. public void doGet(HttpServletRequest request,
  194. HttpServletResponse response)
  195. throws IOException, ServletException
  196. {
  197. <span class="red">response.setContentType("text/html; charset=EUC-JP");</span>
  198. PrintWriter out = response.getWriter();
  199. out.println("&lt;html&gt;");
  200. out.println("&lt;head&gt;");
  201. out.println("&lt;title&gt;HelloWorld&lt;/title&gt;");
  202. out.println("&lt;/head&gt;");
  203. out.println("&lt;body&gt;");
  204. out.println("&lt;p&gt;");
  205. out.println("こんにちは世界");
  206. out.println("&lt;/p&gt;");
  207. out.println("&lt;/body&gt;");
  208. out.println("&lt;/html&gt;");
  209. }
  210. }
  211. </pre>
  212. <p class="dig2">
  213. JAVAはUTF-8で処理を行うので、それ以外のEUC-JPなどを使用する場合は、コンパイルするときに<span class="bold">-encoding</span>をつけること。
  214. </p>
  215. <pre class="dig3 shell">
  216. # <span class="bold">javac <span class="red">-encoding EUC-JP</span> -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar HelloWorld.java</span>
  217. </pre>
  218. <p class="dig1"><span class="title2">JSPの場合</span></p>
  219. <p class="dig2">
  220. ディレクティブでキャラクタセットを指定する。
  221. EUC-JPを使用するなら、&lt;%@ page contentType="text/html; <span class="bold">charset=EUC-JP</span>" %&gt;。
  222. UTF-8を使用するなら、&lt;%@ page contentType="text/html; <span class="bold">charset=UTF-8</span>" %&gt;。
  223. </p>
  224. <pre class="dig3 text">
  225. # hello.jsp
  226. <span class="red">&lt;%@ page contentType="text/html; charset=EUC-JP" %&gt;</span>
  227. &lt;html&gt;
  228. &lt;head&gt;
  229. &lt;title&gt;HelloWorld&lt;/title&gt;
  230. &lt;/head&gt;
  231. &lt;body&gt;
  232. &lt;p&gt;
  233. &lt;%
  234. out.println("こんにちは世界");
  235. %&gt;
  236. &lt;/p&gt;
  237. &lt;/body&gt;
  238. &lt;/html&gt;
  239. </pre>
  240. <p class="dig1"><span class="title2">静的コンテンツ(HTML)の場合</span></p>
  241. <p class="dig2">
  242. <span class="bold">workers2.propeties</span>ファイルで、コンテキストパス以下のすべてのファイルをTomcatに渡すよう設定している場合、静的コンテンツについてもこのページの先頭に書いたようなキャラクタセット情報が返される。
  243. HTMLのmetaタグでcharsetを指定しても無視されるので、ドキュメントルートとコンテキストパスで異なるキャラクタセットを使用したいときは注意が必要。
  244. </p>
  245. <pre class="dig3 text">
  246. # workers2.properties
  247. [uri:/hoge/*] <span class="blue">&lt;---すべてのファイルをTomcatに処理させる</span>
  248. </pre>
  249. <p class="dig2">
  250. 通常は<span class="bold">SetCharacterEncodingFilter</span>を利用するのが常套手段のようだが、どうやってもcharsetを返してくれない。
  251. しかたないので、<span class="bold">web.xml</span>の<span class="bold">&lt;mime-mapping&gt;</span>でcharsetと拡張子の関連付けを指定した。
  252. </p>
  253. <pre class="dig3 text">
  254. &lt;!--(コンテキストパス)/WEB-INF/web.xml--&gt;
  255. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  256. &lt;!DOCTYPE web-app
  257. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  258. "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;
  259. &lt;web-app&gt;
  260. <span class="red"> &lt;mime-mapping&gt;
  261. &lt;extension&gt;html&lt;/extension&gt;
  262. &lt;mime-type&gt;text/html; charset=UTF-8&lt;/mime-type&gt;
  263. &lt;/mime-mapping&gt;</span>
  264. &lt;/web-app&gt;
  265. </pre>
  266. <p class="dig2">
  267. いちおう、<span class="bold">SetCharacterEncodingFilter</span>を利用する方法を書いておくと、$CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes/filtersディレクトリにある<span class="bold">SetCharacterEncodingFilter.java</span>をコンパイルして、
  268. </p>
  269. <pre class="dig3 shell">
  270. # <span class="bold">cd $CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes</span>
  271. # <span class="bold">javac -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar filters.SetCharacterEncodingFilter.java</span>
  272. </pre>
  273. <p class="dig2">
  274. 生成されたクラスファイルを<span class="bold">(コンテキストパス)/WEB-INF/classes/filters</span>ディレクトリにコピーして、<span class="bold">(コンテキストパス)/WEB-INF/web.xml</span>にフィルタの設定を記述するだけらしい。
  275. </p>
  276. <pre class="dig3 text">
  277. &lt;!--(コンテキストパス)/WEB-INF/web.xml--&gt;
  278. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  279. &lt;!DOCTYPE web-app
  280. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  281. "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;
  282. &lt;web-app&gt;
  283. <span class="red">&lt;filter&gt;
  284. &lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;
  285. &lt;filter-class&gt;filters.SetCharacterEncodingFilter&lt;/filter-class&gt;
  286. &lt;init-param&gt;
  287. &lt;param-name&gt;encoding&lt;/param-name&gt;
  288. &lt;param-value&gt;UTF-8&lt;/param-value&gt;
  289. &lt;/init-param&gt;
  290. &lt;/filter&gt;
  291. &lt;filter-mapping&gt;
  292. &lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;
  293. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  294. &lt;/filter-mapping&gt;</span>
  295. &lt;/web-app&gt;
  296. </pre>
  297. <p class="dig2">
  298. うむむ...
  299. </p>
  300. <hr/>
  301. <p class="dig1">
  302. [<a href="/web/20181003202907/http://www.nina.jp/server/index-slackware.html">サーバの実験室 Slackware</a>]
  303. </p>
  304. </body>
  305. </html>
  306. <!--
  307. FILE ARCHIVED ON 20:29:07 Oct 03, 2018 AND RETRIEVED FROM THE
  308. INTERNET ARCHIVE ON 23:27:21 Mar 21, 2022.
  309. JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
  310. ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
  311. SECTION 108(a)(3)).
  312. -->
  313. <!--
  314. playback timings (ms):
  315. captures_list: 133.209
  316. exclusion.robots: 0.289
  317. exclusion.robots.policy: 0.278
  318. RedisCDXSource: 6.152
  319. esindex: 0.009
  320. LoadShardBlock: 108.809 (3)
  321. PetaboxLoader3.datanode: 290.581 (4)
  322. CDXLines.iter: 15.789 (3)
  323. load_resource: 242.738
  324. PetaboxLoader3.resolve: 57.13
  325. -->