tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrey Aristarkhov" <>
Subject JSP encoding problem solution proposal
Date Wed, 08 Aug 2001 14:53:00 GMT
Hi All!

Different encodings support in Servlet/JSP is an ancient well-known problem.
The setCharacterEncoding() method of HttpServletRequest allows to change
encoding before reading parameters. Thus, servlet is able to change encoding
accordance with its needs. (Small lyrical digression: what does this
encoding mean?
I'll post my thoughts about it separately)
Howevet the problem still exists in JSP (there were several postings about
the problem in
this maillist). The purpose of this mail is to propose a solution for
encodings support in JSP.

Problem description
A JSP programmer is not able to change request encoding for incoming JSP
request, since
"This method [setCharacterEncoding] must be called prior to parsing any post
data or
reading any input from the request. Calling this method once data has been
read will
not affect the encoding." (Servlet 2.3 Spec). This happens because request
being read inside org.pache.jasper.servlet.JspServlet, before calling
generated JSP-servlet.
As a result we have the following behaviour of compiled JSP for non-English
1) incoming request being read using 'ISO-8859-1'
2) getParameter() method returns a value in 'ISO-8859-1', but JSP-servlet
suppose the
   return value has JVM default encoding (say "KOI8-R") -- here is ???????
instead of
   real parameter value. Here is a problem.

Problem solution
There should be a configurable optional parameter for JspServlet (say
'requestEncoding') to
change request encoding. According to this parameter JspServlet should call
before processing request. It does not conflict with JSP 1.2 Spec, since
there are now any
words about default encoding of incoming request over there.

I have made neccessary changes to implement this feature in
tomcat-4.0-20010807. It works fine
with different Cyrillic encodings. (Suppose the same result for the rest of
non-Latin1 encodings).
I clearly understand that proposed solution is not a panacea and it's a
subject to discuss.

Andrey Aristarkhov

Diffs are followed (also as attachments). I have also attached a sample JSP
for encoding testing.

file: org/apache/jasper/

>      * Java platform encoding for incoming request.
>      */
>     private String requestEncoding;
>     /**
>     public String getRequestEncoding() {
> 	return requestEncoding;
>     }
>         this.requestEncoding = config.getInitParameter("requestEncoding");

file: org/apache/jasper/

>     /**
>      * Java platform encoding for incoming request.
>      */
>     public String getRequestEncoding();

file: org/apache/jasper/servlet/

<             String includeUri
>             // According to section 4.9 of Servlet 2.3 spec we have to
>             // setCharacterEncoding() before reading any parameter
>             if (options.getRequestEncoding()!=null)
>               request.setCharacterEncoding(options.getRequestEncoding());
>             String includeUri

View raw message