- PR -

óài‚Í‚µ‚²‚j“™‚ÌŠ¿Žš‚ª•¶Žš‰»‚¯‚µ‚Ä‚µ‚Ü‚¢‚Ü‚·EEE

“ŠeŽÒ“Še“à—e
RYO
‰ï‹cŽºƒfƒrƒ…[“ú: 2006/02/21
“Še”: 12
“Še“úŽž: 2006-04-12 17:39
Cafe‚³‚ñ‚ ‚肪‚Æ‚¤‚²‚´‚¢‚Ü‚·B

request.setCharacterEncoding("MS932");
‚É’¼‚µ‚Ä‚â‚Á‚Ä‚Ý‚½‚Ì‚Å‚·‚ªB‘Š•Ï‚í‚炸“¯‚¶Çó‚ªo‚Ü‚·B
‚·‚Ý‚Ü‚¹‚ñEEEB
‚É‚Ì
‰ï‹cŽºƒfƒrƒ…[“ú: 2005/12/15
“Še”: 4
“Še“úŽž: 2006-04-14 02:31
‚±‚¿‚ç‚ÅŽŽ‚µ‚Ä‚Ý‚½‚Æ‚±‚ëA‚±‚±‚ðŽQl‚ÉEncodingFilter‚̐ݒè‚ðs‚¦‚Γo˜^‚Å‚«‚Ü‚µ‚½B
ˆÈ‰º‚ÉŽÀŒ±—p‚̃\[ƒX‚àÚ‚¹‚Ä‚¨‚«‚Ü‚·BŽQl‚É‚Ç‚¤‚¼B


ƒR[ƒh:
yƒVƒXƒeƒ€\¬z
EWindowsXP professional SP2
EInternetExplorer6
EMySQL4.1.12a
ETomcat5.0.28
EJava1.4.2_08


yDB\¬z
ƒ†[ƒU–¼Fabc
ƒpƒXƒ[ƒhFxyz
DB–¼Fmojitest (ƒLƒƒƒ‰ƒNƒ^[ƒZƒbƒg:UTF8)
ƒe[ƒuƒ‹–¼Fmojitest(DDL‚͈ȉº‚Ì’Ê‚è)
CREATE TABLE mojitest (
    message text
) ENGINE=InnoDB;


yWebƒAƒvƒŠ\¬z¦ FƒtƒHƒ‹ƒ_AœFƒtƒ@ƒCƒ‹
 %CATALINA_HOME%
„¤ webapps
@„¤ jsptest
@@„¥œmojitest.jsp
@@„¤ WEB-INF
@@@„¥œweb.xml
@@@„¤ classes
@@@@„¥œEncodingFilter.java
@@@@„¤œEncodingFilter.class ©EncodingFilter.java‚ðƒRƒ“ƒpƒCƒ‹‚µ‚č쐬


yƒtƒ@ƒCƒ‹‚Ì’†gz
œmojitest.jsp
<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J"%>
<%@ page import="java.sql.*" %>
<HTML>
<HEAD><TITLE>•¶Žš“o˜^ƒeƒXƒg</TITLE></HEAD>
<BODY>
<%
    // •Ï”éŒ¾
    String   resultMessage = "";
    String   text = request.getParameter("text");
    Connection con = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql:///mojitest?useUnicode=true&characterEncoding=UTF-8","abc","xyz");
        
        // ƒtƒH[ƒ€‚©‚ç‚̃f[ƒ^‚ª‚ ‚ê‚΃f[ƒ^“o˜^
        if (text != null) {
            if (!text.equals("")) {
                con.createStatement().executeUpdate("INSERT INTO mojitest ( message ) VALUES ( '" + text + "' ) ");
                resultMessage = "‘—Mƒf[ƒ^" + text + "‚ðƒAƒbƒvƒf[ƒg‚µ‚Ü‚µ‚½.";
            }
        }
        
        // DB‚©‚ç‚̃f[ƒ^Žæ“¾ˆ—
        ResultSet rs = con.createStatement().executeQuery("SELECT * FROM mojitest");
%>
DBƒf[ƒ^<BR>
<%
        while (rs.next()) {
%>
<%=rs.getString("message")%><BR>
<%
        }
%>
<BR>
<FORM NAME="testForm" METHOD="post" ACTION="/jsptest/mojitest.jsp">
<TEXTAREA NAME="text"></TEXTAREA>
<INPUT TYPE="submit" VALUE="‘—M">
</FORM>
<%
    } catch (Exception e) {
        String errMessage = "";
        StackTraceElement[] es = e.getStackTrace();
        if (es != null) {
            int i = 1;
            errMessage = es[i].toString();
            while (i < es.length) { 
                errMessage = errMessage + "<BR>@@at " + es[i].toString();
                i++;
            }
        }
        resultMessage = "ƒGƒ‰[‚ª”­¶‚µ‚Ü‚µ‚½.<BR>" + errMessage;
    } finally {
        if (con != null) {
            con.close();
        }
    }
%>
<%=resultMessage%><BR><BR>
</BODY>
</HTML>


œEncodingFilter.java
import java.io.*;
import javax.servlet.*;
public class EncodingFilter implements Filter {
    protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true;
    
    public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
    }
    
    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 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 if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else 
            this.ignore = false;
    }
    
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }
}


œ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>
    <!-- Example filter to set character encoding on each request -->
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>Windows-31J</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>


RYO
‰ï‹cŽºƒfƒrƒ…[“ú: 2006/02/21
“Še”: 12
“Še“úŽž: 2006-04-14 21:34
‚É‚Ì‚³‚ñ‚ ‚肪‚Æ‚¤‚²‚´‚¢‚Ü‚·B

‚É‚Ì‚³‚ñ‚̃\[ƒX‚ðŽQl‚É‚µ‚āAƒtƒBƒ‹ƒ^[‚ðŽg‚¤‚悤Ž©•ª‚̃\[ƒX‚ðC³‚µ‚½‚Æ‚±‚ëA
–³Ž–‚ɉæ–Ê•\Ž¦A“o˜^‚Æ‚à‚É‚Å‚«‚é‚悤‚É‚È‚è‚Ü‚µ‚½B
–{“–‚É‚ ‚肪‚Æ‚¤‚²‚´‚¢‚Ü‚µ‚½B•‚©‚è‚Ü‚µ‚½B

ƒXƒLƒ‹ƒAƒbƒv^ƒLƒƒƒŠƒAƒAƒbƒviJOB@ITj