The Wayback Machine - https://web.archive.org/web/20181003202907/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html

Tomcat��UTF-8/EUC-JP��Ȥ�

[�����Фμ¸��� Slackware]

���� : 2004/12/31

"�����Фμ¸���"�θ���


Tomcat�����֤���륭��饯�����åȤξ���ϡ�httpd.conf�Υ롼�Ȥǻ��ꤷ��AddDefaultCharset���ͤ�Ʊ���ˤʤ�餷���� Directory�ǥ��쥯�ƥ��֤���ǻ��ꤷ��AddDefaultCharset��̵�뤵���äݤ��� �Ĥ��Ǥˡ�meta������̵�뤵���ߤ����� ��<---���Τؤ󡢸�Ҥ�SetCharacterEncodingFilter��������ư��ʤ����Ȥ⤢�ꡢ�����ʤ�...��

������WEB�����Фϥ롼�Ȥ�AddDefaultCharset��EUC-JP����򤷤Ƥ��ꡢ����ƥ����ȥѥ��ʲ���UTF-8�ˤ������Τǡ��ʤ�餫���к��򤷤ʤ���ʸ���������Ƥ��ޤ���

�����֥�åȤξ��

response.setContentType�ǥ���饯�����åȤ���ꤹ�롣 EUC-JP����Ѥ���ʤ顢response.setContentType("text/html; charset=EUC-JP")�� UTF-8����Ѥ���ʤ顢response.setContentType("text/html; charset=UTF-8")��

# HelloWorld.java

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html; charset=EUC-JP");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>HelloWorld</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<p>");
        out.println("����ˤ�������");
        out.println("</p>");
        out.println("</body>");
        out.println("</html>");
    }
}

JAVA��UTF-8�ǽ�����Ԥ��Τǡ�����ʳ���EUC-JP�ʤɤ���Ѥ�����ϡ�����ѥ��뤹��Ȥ���-encoding��Ĥ��뤳�ȡ�

# javac -encoding EUC-JP -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar HelloWorld.java

JSP��

�ǥ��쥯�ƥ��֤ǥ���饯�����åȤ���ꤹ�롣 EUC-JP����Ѥ���ʤ顢<%@ page contentType="text/html; charset=EUC-JP" %>�� UTF-8����Ѥ���ʤ顢<%@ page contentType="text/html; charset=UTF-8" %>��

# hello.jsp

<%@ page contentType="text/html; charset=EUC-JP" %>
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
<p>
<%
    out.println("����ˤ�������");
%>
</p>
</body>
</html>

��Ū����ƥ�ġ�HTML�ˤξ��

workers2.propeties�ե�����ǡ�����ƥ����ȥѥ��ʲ��Τ��٤ƤΥե������Tomcat���Ϥ��褦���ꤷ�Ƥ����硢��Ū����ƥ�ĤˤĤ��Ƥ⤳�Υڡ�������Ƭ�˽񤤤��褦�ʥ���饯�����åȾ����֤���롣 HTML��meta������charset����ꤷ�Ƥ�̵�뤵���Τǡ��ɥ�����ȥ롼�Ȥȥ���ƥ����ȥѥ��ǰۤʤ륭��饯�����åȤ���Ѥ������Ȥ������դ�ɬ�ס�

# workers2.properties

[uri:/hoge/*]    <---���٤ƤΥե������Tomcat�˽���������

�̾��SetCharacterEncodingFilter�����Ѥ���Τ�������ʤΤ褦�������ɤ���äƤ�charset���֤��Ƥ���ʤ��� �������ʤ��Τǡ�web.xml��<mime-mapping>��charset�ȳ�ĥ�Ҥδ�Ϣ�դ�����ꤷ����

<!--�ʥ���ƥ����ȥѥ���/WEB-INF/web.xml-->

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <mime-mapping>
    <extension>html</extension>
    <mime-type>text/html; charset=UTF-8</mime-type>
  </mime-mapping>
</web-app>

����������SetCharacterEncodingFilter�����Ѥ�����ˡ��񤤤Ƥ����ȡ�$CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes/filters�ǥ��쥯�ȥ�ˤ���SetCharacterEncodingFilter.java�򥳥�ѥ��뤷�ơ�

# cd $CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes
# javac -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar filters.SetCharacterEncodingFilter.java

�������줿���饹�ե�������ʥ���ƥ����ȥѥ���/WEB-INF/classes/filters�ǥ��쥯�ȥ�˥��ԡ����ơ��ʥ���ƥ����ȥѥ���/WEB-INF/web.xml�˥ե��륿������򵭽Ҥ�������餷����

<!--�ʥ���ƥ����ȥѥ���/WEB-INF/web.xml-->

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <filter>
      <filter-name>Set Character Encoding</filter-name>
      <filter-class>filters.SetCharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>Set Character Encoding</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

�����...


[�����Фμ¸��� Slackware]