You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by sc <so...@hotmail.com> on 2007/05/29 12:27:57 UTC

WordML encoding problem

There is an earlier post
http://mail-archives.apache.org/mod_mbox/struts-user/200603.mbox/%3C1141255998.8281.29.camel@localhost.localdomain%3E. 
My problem is pretty much the same as described. But there is no soluton to
it.
 
My application is Tomcat + Struts.
In the struts action class, I generates a WordML xml and the browser starts
a word. The word document may contain Chinese character. My source code is
something like:
 
 response.setContentType("application/msword; charset=UTF-8");
 java.io.PrintWriter out = response.getWriter();
 out.println(outLine);   
 out.close();

 
outLine is a big string, with the first 2 lines as the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>

 
Now the weird thing is when I run this on my desktop, with Chinese Windows
XP installed. Everything is fine. But when I run this on a production server
with English version Windows 2003 installed, all chinese characters are
displayed as ??.
 
After I changed the code to:
 response.setContentType("text/xml; charset=UTF-8");
regardless of where I run the application, it can correctly display chinese
characters, though in a text version.
 
Any help is appreciated.
 
SC
-- 
View this message in context: http://www.nabble.com/WordML-encoding-problem-tf3833007.html#a10851477
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: WordML encoding problem

Posted by Laurie Harper <la...@holoweb.net>.
sc wrote:
> Just curious why I don't need to do setCharacterEncoding when I do 
> response.setContentType("text/xml; charset=UTF-8");

You *do* need to call it, if you want to be sure things will work 
everywhere. There are various reasons it might work OK without doing 
that, most likely being that the particular JVM you're testing on is 
already using UTF-8 as the default output encoding.

Always be explicit about character encodings to ensure portability.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: WordML encoding problem

Posted by sc <so...@hotmail.com>.
Yes. It works. The source code is like:
	response.setContentType("application/msword; charset=UTF-8");
	response.setCharacterEncoding("utf-8");
	java.io.PrintWriter out = response.getWriter();

	out.println(outLine);   
	out.close();

Thanks a lot!!! 
Just curious why I don't need to do setCharacterEncoding when I do 
response.setContentType("text/xml; charset=UTF-8");


Laurie Harper wrote:
> 
> sc wrote:
>> There is an earlier post
>> http://mail-archives.apache.org/mod_mbox/struts-user/200603.mbox/%3C1141255998.8281.29.camel@localhost.localdomain%3E. 
>> My problem is pretty much the same as described. But there is no soluton
>> to
>> it.
>>  
>> My application is Tomcat + Struts.
>> In the struts action class, I generates a WordML xml and the browser
>> starts
>> a word. The word document may contain Chinese character. My source code
>> is
>> something like:
>>  
>>  response.setContentType("application/msword; charset=UTF-8");
>>  java.io.PrintWriter out = response.getWriter();
>>  out.println(outLine);   
>>  out.close();
>> 
>>  
>> outLine is a big string, with the first 2 lines as the following:
>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>> <?mso-application progid="Word.Document"?>
>> 
>>  
>> Now the weird thing is when I run this on my desktop, with Chinese
>> Windows
>> XP installed. Everything is fine. But when I run this on a production
>> server
>> with English version Windows 2003 installed, all chinese characters are
>> displayed as ??.
>>  
>> After I changed the code to:
>>  response.setContentType("text/xml; charset=UTF-8");
>> regardless of where I run the application, it can correctly display
>> chinese
>> characters, though in a text version.
> 
> It sounds like you've answered your own question. Your first code 
> fragment writes out an XML document that declares its character encoding 
> as UTF-8, but you don't have any code to ensure the response uses that 
> encoding.
> 
> Calling setContentType() is only part of the correct solution, though. 
> You should also call setCharacterEncoding() on the response. 
> setContentType() helps the user agent (browser, etc.) to figure out what 
> character encoding it should assume is in use; setCharacterEncoding() 
> ensures that a particular encoding *is* used. The 'encoding' attribute 
> in your XML declaration must then match that encoding.
> 
> HTH,
> 
> L.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/WordML-encoding-problem-tf3833007.html#a10866269
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: WordML encoding problem

Posted by Laurie Harper <la...@holoweb.net>.
sc wrote:
> There is an earlier post
> http://mail-archives.apache.org/mod_mbox/struts-user/200603.mbox/%3C1141255998.8281.29.camel@localhost.localdomain%3E. 
> My problem is pretty much the same as described. But there is no soluton to
> it.
>  
> My application is Tomcat + Struts.
> In the struts action class, I generates a WordML xml and the browser starts
> a word. The word document may contain Chinese character. My source code is
> something like:
>  
>  response.setContentType("application/msword; charset=UTF-8");
>  java.io.PrintWriter out = response.getWriter();
>  out.println(outLine);   
>  out.close();
> 
>  
> outLine is a big string, with the first 2 lines as the following:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <?mso-application progid="Word.Document"?>
> 
>  
> Now the weird thing is when I run this on my desktop, with Chinese Windows
> XP installed. Everything is fine. But when I run this on a production server
> with English version Windows 2003 installed, all chinese characters are
> displayed as ??.
>  
> After I changed the code to:
>  response.setContentType("text/xml; charset=UTF-8");
> regardless of where I run the application, it can correctly display chinese
> characters, though in a text version.

It sounds like you've answered your own question. Your first code 
fragment writes out an XML document that declares its character encoding 
as UTF-8, but you don't have any code to ensure the response uses that 
encoding.

Calling setContentType() is only part of the correct solution, though. 
You should also call setCharacterEncoding() on the response. 
setContentType() helps the user agent (browser, etc.) to figure out what 
character encoding it should assume is in use; setCharacterEncoding() 
ensures that a particular encoding *is* used. The 'encoding' attribute 
in your XML declaration must then match that encoding.

HTH,

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org