プロが教えるわが家の防犯対策術!

サーバ Fedora Core 6 / tomcat5.5

クライアントもFedoraなら文字化けしません。
ウィンドウズxpから接続すると文字化けします。
アプレットのプログラムで送信する関数のみ抜粋した。引数sが送信する文字列でこれが文字化けします。送信した変数sをJLabelで表示するとウィンドウズでも文字化けしてなかった。そしてサーバー側のログは文字化けしていたのですが原因がわかりません。どうしたらいいですか?

//■アプレット側 文字列送信用関数
public void send(String s)
{
try
{
sock = new Socket(サーバのIPアドレス,9999);

//■サーバーに接続
fin = sock.getInputStream();
fout = sock.getOutputStream();
ffin = new InputStreamReader(fin);
in = new BufferedReader(ffin);
out = new PrintWriter(fout,true);

out.println("POST / HTTP/1.1");
out.println("Accept: */* ");
out.println("Accept-Language: ja");
out.println("Accept-Encoding: gzip,deflate");
out.println("User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
out.println("Host: "+getParameter("ipAddress"));

out.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
out.println("Content-Length: "+(s.getBytes("UTF-8").length));

out.println("Connection: Keep-Alive");
out.println("");

out.println(s);
}
catch(IOException e)
{
}
}

サーバー側 変数sの表示
public void service(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
//■通信関係
PrintWriter out;
BufferedReader in;

//■入力した文字列
String st,str;

try
{
//■通信開始
in = new BufferedReader(new InputStreamReader(req.getInputStream()));
out = new PrintWriter(res.getOutputStream(),true);

//■文字列読み込み
st = in.readLine();
str = new String(st.getBytes("UTF-8"));

System.out.println("受信した文字列"+str);

A 回答 (2件)

Servletを利用している場合、クライアントとの文字コードの差を吸収するには、主にフィルタを利用します。



クライアントの文字コードがSJISの場合、HttpServletRequestの文字コードがSJISになっています。
その為、Servlet側がUTF-8の場合、HttpServletRequestの文字コードをsetCharacterEncodingで変更してあげる必要があります。

これを全てのServletに記述するのは、メンテナンス性が非常に悪いので、Servletの処理が行う前にフィルタを使用して文字コード変換処理を噛ませてあげれば、フィルタが指定されているServlet全ての文字コードを変換することが出来ます。

↓のURLを参考にしてみて下さい。

参考URL:http://www.hellohiro.com/filter.htm
    • good
    • 0

送信側と受信側の文字エンコードに食い違いがあるからでしょ。

この回答への補足

どうしたらいいですか?
どこを変えればいいですか?
どの部分をどうかえればいいですか?

補足日時:2007/04/02 23:06
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!