You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Marczisovszky Daniel <ma...@dev-labs.com> on 2002/03/28 21:39:59 UTC

Problem with forward() and setCharacterEncoding

Hi,

My situation is the following: I have to servlets, let's call them
Forwarder and Test. I send requests (both GET and POST) to the
Forwarder servlet which uses request.getRequestDispatcher to forward
it to the Test servlet. I'm using Tomcat 4.0.3, but 4.0.4 produced the
same.

The problem is the following: when the Test servlet receives the
request in doGet or doPost, or even in service(), the parameters are
already parsed according to the ISO-8859-1 character encoding. This
makes impossible to specify the character encoding with
request.setCharacterEncoding() in the Test servlet.

Actually I can't tell where is it implemented exactly, because the
source code is too big for me, but I read the Servlet specification
and probably I know why this happens and what may be a workaround for
it.

The Servlet API 2.3 Specification says on page 56: "Parameters
specified in the query string used to create the RequestDispatcher
take precedence over other parameters of the same name passed to the
included servlet. The parameters associated with a RequestDispatcher
are scoped to apply only for the duration of the include or forward
call."

So when forwarding to a path "/servlet/Test?id=5678" the original id
parameter is overwritten with value 1234, if, for example the original
request was /servlet/Forwarder?id=1234.

I guess this overwriting is made by parsing the parameters in the
original request and parsing the parameters in the path specified for
the Dispatcher and after that they are merged. But at this point, the
original parameters are encoded with ISO-8859-1, and I'm loosing
valuable accented characters... Moreover, even posted parameters are
parsed, although the specification is not clear about it, but on page
58 (SRV.8.4.1) it says "The request dispatching mechanism is
responsible for aggregating query string parameters when forwarding or
including requests."

IMHO this means only query strings should be merged, but posted data
not.

Additionally the ServletInputStream is completely read (0 bytes can be
read from it) in the Test servlet, so I even can't reparse the
parameters.

My suggestion is that neither path nor the original query string
should be URLDecoded. Only those parts should be overwritten in the
original query string in which the parameter name equals to any in the
path string. Parameter parsing should be done in Test servlet, not in
Forwarder servlet.

Best wishes,
Daniel Marczisovszky


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>