File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes character encoding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "character encoding problem" Watch "character encoding problem" New topic
Author

character encoding problem

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

I have a servlet running on Tomcat 5.5.7 container on windows Japanese OS. The servlet receives a HTTP POST request with its URL UTF-8 encoded. I am not able to read the characters properly(i am getting junk) even though I have URIEncoding="UTF-8 in conf\server.xml file. I tried to setting req.setCharacterEncoding("UTF-8") but it does not work.I am going mad.


Groovy
Ketan Khairnar
Ranch Hand

Joined: Jun 28, 2005
Posts: 34
Request Encoding
The request encoding is the character encoding in which parameters in an incoming request are interpreted. Currently, many browsers do not send a request encoding qualifier with the Content-Type header. In such cases, a Web container will use the default encoding�ISO-8859-1�to parse request data. If the client hasn�t set character encoding and the request data is encoded with a different encoding from the default, the data won�t be interpreted correctly. To remedy this situation, you can use the ServletRequest.setCharacterEncoding(String enc) method to override the character encoding supplied by the container. You must call the method or tag before parsing any request parameters or reading any input from the request. Calling the method or tag once data has been read will not affect the encoding.


Response Encoding
This is required to get multilingual data on UI side of our application.
The setCharacterEncoding, setContentType, and setLocale methods can be called repeatedly to change the character encoding. Calls made after the servlet response�s getWriter method has been called or after the response is committed have no effect on the character encoding. Data is sent to the response stream on buffer flushes (for buffered pages) or on encountering the first content on unbuffered pages. Calls to setContentType set the character encoding only if the given content type string provides a value for the charset attribute. Calls to setLocale set the character encoding only if neither setCharacterEncoding nor setContentType has set the character encoding before. For Java Servlets, you can specify a page encoding in the HTTP header by calling the setContentType() method of the Servlet API. The following doGet() function shows how this method should be called.



Hope this helps. Just check where u made mistake. and Let us know
Kia Phia Ben
Ranch Hand

Joined: Dec 16, 2005
Posts: 48
Hi Pradip Bhat
Add one line in servlet
request.setCharacterEncoding("utf-8");
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Kia Phia Ben:
Hi Pradip Bhat
Add one line in servlet
request.setCharacterEncoding("utf-8");


I have tried that, it did not work. I dont know why it did not work.
Ketan Khairnar
Ranch Hand

Joined: Jun 28, 2005
Posts: 34
have you read what I have written here before?
that should work.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

You must call the method or tag before parsing any request parameters or reading any input from the request. Calling the method or tag once data has been read will not affect the encoding.



Yes.. this is first line of code.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Can you go to http://7bee.j2ee.us/ChatHouse/chat/ and enter to Atic room? Then you can try to enter Japanese text and URLs and tell if you can see them correctly back.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

There was some problem with Tomcat installation. I installed Tomcat again and everything is working fine.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Sorry guys. It was not a problem with Tomcat. Previously I was running Tomcat as Windows service with JRE version 1.5.0_05. Later I used startup.bat for ruuning Tomcat which was using a JDK v 1.5.0_1 and it worked fine. Looks like there is some bug with JRE 1.5.0_05. I started Tomcat as a windows service with the latest JRE version 1.5.0_06 and it is working fine. Thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: character encoding problem
 
Similar Threads
fmt:setBundle changes the response's encoding. HELP!
difference work of between init() and constructor?
Decoding UTF-8 string in Servlet
Money and house...
Thread Synchronization??