- PR -

サーブレットで文字化け

投稿者投稿内容
and1
会議室デビュー日: 2007/05/27
投稿数: 13
投稿日時: 2007-06-10 22:35
杏様

ご回答ありがとうございます。

>HttpServletResponse#setCharacterEncoding(java.lang.String)

HttpServletRequest#setCharacterEncoding(java.lang.String)の事ですね。
今回のプログラムはリクエストパラメータから文字列を取得するわけではないので
このメソッドの代わりに String#getBytes(String charsetName)を使って
エンコーディングしました。
結果はうまく日本語をブラウザに表示できるようになりました。

残りの問題はコンパイル時に"あ"が"?"になってしまう事です。
もうひといきで問題解決です。

もう少し知恵を貸していただけないでしょうか。
よろしくお願いします。



朝日奈ありす
大ベテラン
会議室デビュー日: 2007/05/02
投稿数: 189
お住まい・勤務地: 最北の地
投稿日時: 2007-06-11 07:21
APIの該当メソッド部ぺたっ
コード:
void javax.servlet.ServletResponse.setCharacterEncoding(String charset)

setCharacterEncoding
public void setCharacterEncoding(java.lang.String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8. 
This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed. 

Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

Parameters:
charset - a String specifying only the character set defined by IANA Character Sets (http://www.iana.org/assignments/character-sets)
Since: 
2.4 
See Also: 
#setLocale

and1
会議室デビュー日: 2007/05/27
投稿数: 13
投稿日時: 2007-06-13 08:53
杏様

いつもありがとうございます。

>HttpServletResponse#setCharacterEncoding
失礼しました、こんなメソッドが存在したのですね。
知りませんでした。

この方法の方がよさそうですね。
明日以降、試してみたいと思います。

後日また書き込みいたします。
and1
会議室デビュー日: 2007/05/27
投稿数: 13
投稿日時: 2007-06-20 12:58
問題点を絞り込みたいため
一度、質問を締め切って
後日、新たなスレッドを立てたいと思います。

ご回答していただいた皆様
ありがとうございました。

杏様改め、仙堂ありす様
には特によいアドバイスを頂きまして感謝しております。
今後ともよろしくお願いします。

スキルアップ/キャリアアップ(JOB@IT)