0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【java】jspで表示する日本語文字が文字化けしてしまうエラーの対処法

Posted at

◆エラー内容

レスポンス用のjspで表示する日本語文字が文字化けした。
英語文字は問題なく表示される。

◆サーブレットのdoPost、doGetの内容

  • doGetの内容
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request, response);
}
  • ◆doPostの内容
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
	String text = request.getParameter("textName");
	text += "hogehoge world!";
	request.setAttribute("responseText", text);
	request.getRequestDispatcher("response.jsp").forward(request, response);
}

◆エラーの原因

リクエストとレスポンスの文字コード設定を行っていなかったため。

◆修正点

doPostメソッド内に以下の部分を追記してエラーは解消された。

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

修正後のdoPostメソッドの全文は以下の通りです。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
		
	String text = request.getParameter("textName");
	text += "hogehoge world!";
	request.setAttribute("responseText", text);
	request.getRequestDispatcher("response.jsp").forward(request, response);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?