3

I have a jsp page running under jboss 4.2.2 server.

The structure for the page is something like this:

include head ( head is written on another page, like masterpage in aspx. )
(body ( where the problem appears ))
include foot ( foot is also written in another page. )

The head page contains the encoding and meta tags:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

When I write characters in the page such as şğĞİÇçÖ (Turkish) the characters are shown as "?" ( question mark ) what should I do to avoid this behavior?

How can I have the text shown just as writen in the jsp page?

Caleb
  • 5,084
  • 1
  • 46
  • 65
Olgun Kaya
  • 2,519
  • 4
  • 32
  • 46
  • "when I write something in the page" - I guess just writing it doesn't cause problems. Perhaps submitting, storing in DB, or anything else is causing this. Clarify your question. – Bozho Feb 24 '11 at 09:23
  • I mean hardcoded in the page like şğüçö – Olgun Kaya Feb 24 '11 at 09:24

3 Answers3

6

I see two potential causes:

  1. Your editor didn't save the page as UTF-8. Check the default settings and/or the Save As option.
  2. The @page is missing in some of the JSPs. It has to be present in all JSP files, also the includes.

Unrelated to the concrete problem, the following in top of JSP was been enough:

<%@ page pageEncoding="UTF-8" %>

The remnant which you've put there are already (implicit) defaults.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • actually there is just a global page say head.jsp and it includes those settings. how can I check such options in eclipse ? – Olgun Kaya Feb 25 '11 at 13:03
  • I reiterate: it has to be present in **all** JSP files. As to Eclipse, check *General > Workspace > Text file encoding* in preferences. – BalusC Feb 25 '11 at 13:09
2

In addition to the DOCTYPE declaration it is usually a good idea to include a meta tag in the document <head> like this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Beyond that BalusC's suggestion that all files involved need to be actually SAVED with UTF8 encoding is very important.

Community
  • 1
  • 1
Caleb
  • 5,084
  • 1
  • 46
  • 65
  • That meta tag is **ignored** when the page is served over HTTP. Instead, the charset will be obtained from the HTTP response header. The JSP page encoding in my answer does exactly that. The OP only has to ensure that it's been set in all JSPs. See also http://www.w3.org/TR/html4/charset.html#h-5.2.2 and http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#JSPServletResponse – BalusC Aug 05 '11 at 14:55
1

Add this snippet into your servlet:

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html; charset=utf-8");
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
U O
  • 29
  • 3