You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Paul Darling <pa...@tumbleweed.com> on 2001/01/02 20:42:01 UTC
RE: Struts handling of multi-lingual input/output strings
Craig,
Thanks for the response. One point I wasn't clear on. You say that on a
POST the Reader uses the "Content-Type" of the request. But then later you
say that the content-type of the JSP has nothing to do with how the
subsequent *input* is interpreted. So does this mean that there is no
control that can be applied using the JSP definition to the content type of
the parameters input to the page?
Also, are there any changes on the horizon that would affect this support of
multinational character encodings?
thanks,
Paul Darling
-----Original Message-----
From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
Sent: Friday, December 29, 2000 12:34 PM
To: struts-dev@jakarta.apache.org
Subject: Re: Struts handling of multi-lingual input/output strings
Michael Westbay wrote:
> Darling-san wrote:
>
> > I'd like to understand how Struts handles different character encodings
that
> > may be used by a Web browser.
>
> This is something that I've been wondering about myself.
>
As one of the authors of a servlet container (Tomcat :-), I can offer some
insight
into what really happens.
>
> > When Struts calls setter methods to set string values, I assume that the
> > strings will in all cases be encoded as Unicode strings. Is this
correct?
>
> That was my assumption as well. But after experimenting a bit, I've found
> that it may not be the case. Strings taken from properties files (and
within
> jsp pages for that matter) in the system's default character set (EUC-JP
in
> my case) get passed through fine. Doing a native2ascii on the properties
and
> using them doesn't. Using Shift_JIS encoding in .jsp and/or properties
also
> doesn't pass through correctly.
>
It is correct to say that, by the time property setters are called, we are
talking
about Unicode strings. That is because Java uses Unicode internally for
*all*
strings.
The issue, though, is "what conversion is used to convert request parameters
into
Unicode strings?" Unfortunately, the answer varies depending on
circumstances,
and is not always satisfactory.
If you are doing a POST request, the Reader used to process the request
parameters
uses the character encoding specified as part of the "Content-Type" header,
if
there is one. If not, the platform default (often ISO-8859-1) is used.
If you are doing a GET request, there is no mechanism within the HTTP
protocol
specification to indicate what character encoding should be used. :-(
In the servlet 2.3 specification, a new method was added to the
ServletRequest
interface -- setCharacterEncoding(). This can be used by a servlet that
wants
to
choose what character encoding to employ, no matter what the request says or
does
not say. As above, it will only influence what happens on a POST. And, you
have
to call it before any of the getParameter() family of methods are called.
>
> What I'm not sure about, though, is if the Reader used in getting the
> properties is making the coversion to Unicode or not. I was under the
> impression that it was simply a ResourceBundle underneith - which has
always
> worked properly with native2ascii-ed files before. This is an area that I
> keep wanting to look into further - if I can find the time.
>
For properties files, this is correct -- you need to run native2ascii first
to
make sure that they are read correctly.
>
> > Doesn't Stuts use the pageEncoding value of the .jsp file to determine
the
> > character encoding of input strings, and also convert output strings
from
> > Unicode to the encoding specified in the form used in a response?
>
> I experimented a bit, but couldn't find any evidence to support this.
With
> Japanese it's especially difficult, since there are multiple character
sets
> possible for the ja_JP locale. For input strings, I usually use the
Japanese
> "autodetect" encoding. But I haven't found how to specify the output
> encoding.
>
The "pageEncoding" attribute is new in JSP 1.2, and defines the character
encoding
of the JSP page itself, as it is read by the compiler. It has nothing to do
with
how an incoming request to that page is interpreted.
>
> With 2.0 servlets, I would just take the output stream and apply an
encoding
> to it by hand (defined in a properties file). But with Struts, it doesn't
> appear that one can change the encoding of the output in mid-stream - so I
> haven't been able to have any control over it. And, yes, I've tried
defining
> the encoding in the xml header at the top of the jsp pages. That doesn't
> appear to make any difference.
>
You should be able to set the character encoding for your *output* of a JSP
page
by saying something like:
<%@ page ... contentType="text/html;charset=EUC-JP" ... %>
but, again, this has nothing to do with how subsequent *input* is
interpreted.
>
> I'm really not sure if this is a Struts problem or a Tomcat (or whatever
> application server you're using) problem.
>
> What I'm currently thinking is that I'll have to chain an encoding filter
> to the Writer in order to gain any control over its output. But if the
> Writer is already doing a coversion, I'm afraid that it'll all get garbled
> in the end.
>
> For the time being, encoding everything (properties and jsps) in EUC
works.
> But it's something I'd like to understand better before getting to the
point
> where I have to ship something.
>
> --
> Michael Westbay
>
Craig McClanahan
Re: Struts handling of multi-lingual input/output strings
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Paul Darling wrote:
> Craig,
>
> Thanks for the response. One point I wasn't clear on. You say that on a
> POST the Reader uses the "Content-Type" of the request. But then later you
> say that the content-type of the JSP has nothing to do with how the
> subsequent *input* is interpreted. So does this mean that there is no
> control that can be applied using the JSP definition to the content type of
> the parameters input to the page?
>
My understanding is this -- the content type and character encoding used on the
POST request are totally up to the browser, and not under control of the server
application at all.
>
> Also, are there any changes on the horizon that would affect this support of
> multinational character encodings?
>
In servlet 2.2, not really.
In servlet 2.3, a method -- ServletRequest.setCharacterEncoding() -- was added
so that the application could make its own decision about what character
encoding to use when reading the data content of the request. If this is called
before the POST parameters are read, it will affect how they are interpreted.
Once 2.3 is implemented more widely, we'll probably look at adding a
configuration option for this to the Struts controller servlet.
Aso of 2.3, the default character encoding (if none is specified) is also
defined by the spec to be "ISO-8859-1". In previous versions, this was not
clearly defined, and some containers defaulted to the server platform's default
encoding instead.
>
> thanks,
> Paul Darling
>
Craig