• Post Reply Bookmark Topic Watch Topic
  • New Topic

CharacterEncoding  RSS feed

 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I probably noticied that j2ee/sdk1.2.1 does not have the setCharacterEncoding() method.

I get Compared with these links :

@Link :

http://java.sun.com/j2ee/sdk_1.2.1/techdocs/api/index.html

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/index.html

Currently in my servlet when i used

request.getCharacterEncoding() it reurned "ISO-8859-1".

#question - 1

Is this UTF-1 ?

I need to use utf-8 in my servlet but am using j2ee/sdk1.2.2 which do not have the setCharacterEncoding() method !

SO, I cannot set UTF-8 ! Is this right ?

#question - 2

Is there any other way to solve the problem !

#question - 3

Also, would some one brief me about what is

ISO-8859-1 is this an equivalent to UTF-1

if so,

what is the equivalet of UTF-1 ? IS0-???-??


Could some one Brief me with this with respect to java in general if not by servlets.

That's the reason i posted this topis here !
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram,
ISO-8859-1 is a character repertoire which uses an 8 bit
encoding (It is also known the Latin-1 character set).

(ISO-8859-1 is not UTF-1. UTF-1 is an encoding scheme for
Unicode such as UTF-8 or UTF-16. Let's forget UTF-1 for
the rest of the discussion as it is not relevant)

When request.getCharacterEncoding() reurned "ISO-8859-1" that means
the character encoding of the request is ISO-8859-1. Since the request
object is already aware of the incoming encoding, when you read the request
these should correctly be read to Java UTF-16 strings.

Now sending back the response to the User Agent is concerned with
the HttpServletResponse. If you check the API (j2ee 1.2.1) for
the getCharacterEncoding method...


Returns the name of the charset used for the MIME body sent in this response.
If no charset has been assigned, it is implicitly set to ISO-8859-1 (Latin-1).



Right, so if you simply use the response object the encoding of the
response will be ISO-8859-1.
You can change this default encoding in the following way.
(Where "res" below is the HttpServletResponse object.)


PrintWriter out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);
out.print(String s);

I think this should work. Try it out let us know how it comes through.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check here to learn more about character encodings etc.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gamini Sirisena:
Also check here to learn more about character encodings etc.


That was worth 1000 pounds! I have been looking for a solution for the past 3 days !

Thanks a lot that you replied !

Will try that and will discuss soon !

 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gamini Sirisena:
Also check here to learn more about character encodings etc.


Thanks for the help with response objects.

My requirement is to read the request parameters in UTF-8 format.

Is there a way to do with J2EE/sdk1.2.2.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is to read the request parameters in UTF-8 format.

Is there a way to do with J2EE/sdk1.2.2.


If you retrieve the parameters as strings, then you don't need to worry about this, as the container handles it for you.
You can create a byte[] in any encoding you wish, but why would you want to do that? Do you want to store the parameters someplace else?
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:


If you retrieve the parameters as strings, then you don't need to worry about this, as the container handles it for you.
You can create a byte[] in any encoding you wish, but why would you want to do that? Do you want to store the parameters someplace else?


No ! Actually , am sending the received values to another application, (finacle), a web application that handles Transactions.

I need to send all request parameters received from a page as a stringline of the UTF-8 format to that application, since, that understands only UTF-8

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you sending the string to the other application? Unless you're using binary serialization, what is sent will not be a Java string object, but a byte[], and you'll need to specify the proper encoding. The String.getBytes(String) method can do that.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
How are you sending the string to the other application? Unless you're using binary serialization, what is sent will not be a Java string object, but a byte[], and you'll need to specify the proper encoding. The String.getBytes(String) method can do that.


Hi Ulf,

Let me give the clear inputs and the outputs.


Thanks for your efforts.

What am doing is exactly a single character conversion from Binary values

Please verify this line

Binary - hexadecimal - integer - char
11110000 - F0 - 240 - ��


This is what am doing / In this sequence .

from Binary -> converting to -> hexadecimal ->converting to -> Integer ->
converting to -> to Char.

Am not getting the expected output. Am getting "actual Output"

verify this table

I will give the exact

Expected Output - Input sequence below:
(Please note before hyphen - is the correct output that am expecting - after hyphen is the exact integer value of the hexadecimal "F0")



Am not getting an output like this ! am getting some other character set when i verified in the net, i got confirmed that the char-set that i tend to receive is of latin-1 format.

This output shown above is the charset that i want and it is of UTF-8 format.

i verified this link for the table.

http://www.echip.com.vn/echiproot/weblh/suutam/2000/uformat.htm

Am not able to figure out where the problem is exactly.

Am using this code :






The output table shown above is from the applogs of the unix machine.

My actual outputs are received from the windows system.

Will this is the cause for the problem !
Thanks .

Please help me find a solution.
[ September 25, 2008: Message edited by: ram kumar ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand that table. Can you post the code you're using to do this?
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
I don't really understand that table. Can you post the code you're using to do this?


I have updated the code too.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't say that I follow the intent of the code, but I'm getting the impression that you're making it way harder on yourself than it needs to be.

If you want to convert "11110000" to 240, then that's what Integer.parseInt("11110000", 2) does. That number can be cast to a char if need be.

If you want to transfer a string as UTF-8 to some destination, you can get the bytes to transfer using String.getBytes("UTF-8").

Does this help?
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the glyph you expect to see for F0 (integer 240)? (The unicode code point for this glyph is 00F0)

 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
I can't say that I follow the intent of the code, but I'm getting the impression that you're making it way harder on yourself than it needs to be.

If you want to convert "11110000" to 240, then that's what Integer.parseInt("11110000", 2) does. That number can be cast to a char if need be.

If you want to transfer a string as UTF-8 to some destination, you can get the bytes to transfer using String.getBytes("UTF-8").

Does this help?


Hi Ulf !

I have started working over the code, will get back to you in some 3 hrs.

 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output table shown above is from the applogs of the unix machine.

My actual outputs are received from the windows system.


Are you saying that in unix you can see the unicode chars correctly but when you view them under windows they are not what you expect? Could you give some more information on these lines? e.g what viewers you are using etc?
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gamini Sirisena:


Are you saying that in unix you can see the unicode chars correctly but when you view them under windows they are not what you expect? Could you give some more information on these lines? e.g what viewers you are using etc?


Watch my new post with Code !

http://www.coderanch.com/forums/f-33/java
[ September 26, 2008: Message edited by: ram kumar ]
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
I can't say that I follow the intent of the code, but I'm getting the impression that you're making it way harder on yourself than it needs to be.

If you want to convert "11110000" to 240, then that's what Integer.parseInt("11110000", 2) does. That number can be cast to a char if need be.

If you want to transfer a string as UTF-8 to some destination, you can get the bytes to transfer using String.getBytes("UTF-8").

Does this help?


Hi Ulf !

Please watch my new code . . . you will get a better understanding !

http://www.coderanch.com/t/412036/java/java/simple-code-char-conversion
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!