servlet printing unicode

[nobr]i am just trying to print a set of turkish charcters for test purposes. following code throws an exception thats where i am checking if characters are printed correctly. even thoug i set the content-type to utf-8 tomcat doesn't set it i get the following as my headers. i also tried some other encodings just to check if it changes to another value with no luck. am i missing something?

HTTP/1.x 200 OK

Server: Apache-Coyote/1.1

Content-Type: text/html;charset=ISO-8859-1

Content-Length: 224

Date: Sun, 28 Jan 2007 09:37:58 GMT

publicvoid doGet(HttpServletRequest request, HttpServletResponse

response)throws IOException{

try{

out = response.getWriter( );

response.setContentType("text/html;charset=UTF-8");

response.setCharacterEncoding("UTF-8");

// Assign security manager

if (System.getSecurityManager() ==null)

System.setSecurityManager

(new RMISecurityManager());

service = (Manager) Naming.lookup

("rmi://127.0.0.1/ManagerServer");

Boolean validQuery = parseQuery( request );

printHeader( );

if ( validQuery ==false ){

out.println("<br>

Invalid Query.. " );

return;

}

finallong start = System.currentTimeMillis();

searchResult = service.doSearch(query , resultBegin , resultEnd );

String timeTook =Long.toString

( System.currentTimeMillis() - start );

printResults( timeTook );

//print footer

printFooter( );

}catch(Exception e){

out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-9\">");

out.println("\u0130\u0131\u00D6\u00F6\u00DC\u00FC\u00C7\u00E7\u0aaE\u011F\u015E\u015F");

out.println( e );

return;

}

}

[/nobr]

[2702 byte] By [nakkayaa] at [2007-11-26 16:27:21]
# 1

Using apache-tomcat-5.5.20.

I have just modified one of my servlets to display your Turkish string. If I useString encoding= "utf-8";

res.setContentType("text/html;charset=" + encoding);

res.setCharacterEncoding(encoding);

then I get the string displayed correctly. If I change the encoding to "iso-8859-9" then one character '\u00E7' is not displayed correctly but all the rest are.

P.S. I hope this code is not going into production because 'out', 'service' and 'searchResult' seem to be declared as servlet instance variables which makes the servlet not thread safe.

Message was edited by:

sabre150

sabre150a at 2007-7-8 22:51:33 > top of Java-index,Java Essentials,Java Programming...
# 2

did your http header changed if i change text/html to text/xml or text/cvs contentType header change but charset is still set to iso8859-1 is there a hard coded value in tomcat that overrides mine?

> P.S. I hope this code is not going into production

> because 'out', 'service' and 'searchResult' seem to

> be declared as servlet instance variables which makes

> the servlet not thread safe.

that comes from my misunderstanding of servlets i thougth every client gets a fresh copy of the class just like running from command line. i was having concurency issues thanks for pointing out.

nakkayaa at 2007-7-8 22:51:33 > top of Java-index,Java Essentials,Java Programming...
# 3

> did your http header changed if i change text/html to

> text/xml or text/cvs contentType header change but

> charset is still set to iso8859-1 is there a hard

> coded value in tomcat that overrides mine?

If I use text/xml I get the charset I setup. What makes you think you are getting iso-8859-1?

sabre150a at 2007-7-8 22:51:33 > top of Java-index,Java Essentials,Java Programming...
# 4

i check it using a firefox plug in ( live http headers ) here is the full http transaction..

http://localhost:8080/servlet/com.lacrone.webui.search

GET /servlet/com.lacrone.webui.search HTTP/1.1

Host: localhost:8080

User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9

Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Cookie: JSESSIONID=45jj5f8m1ow8n

Cache-Control: max-age=0

HTTP/1.x 200 OK

Server: Apache-Coyote/1.1

Content-Type: text/html;charset=ISO-8859-1

Content-Length: 219

Date: Sun, 28 Jan 2007 14:54:36 GMT

in the contentType header charset is always set to iso-8859-1 no matter what i chage it iin to.

nakkayaa at 2007-7-8 22:51:33 > top of Java-index,Java Essentials,Java Programming...
# 5
setting encoding before calling getWriter method solved the problem all characters are printing fine.
nakkayaa at 2007-7-8 22:51:33 > top of Java-index,Java Essentials,Java Programming...