You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Tim Koop <gr...@timkoop.com> on 2005/07/21 20:34:42 UTC

Request parameter data not being recognised as UTF-8

Hi everyone.

___ The Problem ___

I have a web page form that is expecting UTF-8 data, but when my code 
gets the data, the getParameter() methods don't return the right thing.  
I have to run this code to get the right thing:

String value = new 
String(request.getParameter("fieldName").getBytes("ISO-8859-1"), "UTF-8");

Naturally I don't want to run this code every time I want a simple 
parameter.  There must be a setting somewhere.

The page itself displays fine; the form data that it gives me is not 
being interpretted as UTF-8.


___ What I have tried ___

I have searched the web and have found these possible solutions, but I 
can't get them to work for me.  I have tried all the following things:

In server.xml, I have added these lines to each Connector:
  URIEncoding="UTF-8"  useBodyEncodingForURI="false"
  (I have also tried useBodyEncodingForURI="true")

I have added this text to JAVA_OPTS (in my rc.d init script) and have 
confirmed that these are indeed Java system properties
 -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8

I have tried calling request.setCharacterEncoding("UTF-8") in my code.  
I've even made it the first line of the jsp page.  No luck.

I have restarted Tomcat many times.

I even have the environment variable LANG set to "en_GB.UTF-8"

I have <%@ page contentType="text/html; charset=UTF-8"  
pageEncoding="UTF-8" %> in my jsp page.
I have <meta http-equiv="Content-Type" content="text/html; 
charset=utf-8"> in my jsp page.

___ My questions ___

Should this actually work and I'm just doing something wrong?

Is there anything else I can try?


___ Technical details ___

I'm using Tomcat 4.1.30, Apache httpd 1.?, mod_jk, Sun JVM 1.4.2, Linux 
Fedora Core 2.4.22

A part of my server.xml file looks like this:
    <!-- Define a Coyote/JK2 AJP 1.3 Connector on port 8009 -->
    <Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
               port="8009" minProcessors="5" maxProcessors="75"
               enableLookups="true" redirectPort="8443"
               acceptCount="10" debug="0" connectionTimeout="0"
               useURIValidationHack="false"
               
protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler"
               URIEncoding="UTF-8"
               useBodyEncodingForURI="false" />



Thanks for any ideas.  I've tried everything I can think of.

--
Tim Koop

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


Re: Request parameter data not being recognised as UTF-8

Posted by Martin Bromley <ma...@sustainable-energy.co.uk>.
If you take a look at the API docs for ServletRequest.setCharacterEncoding(), they say 

"This method must be called prior to reading request parameters or reading input using getReader()."

By the time execution gets to your JSP page, something has probably read your request parameters - maybe an MVC controller servlet of some sort.

Martin

Tim Koop wrote:
> Thanks Mikolaj.  That did it.
> 
> For some reason running request.setCharacterEncoding("UTF-8") in my code 
> does nothing, but if it gets run from a Filter it works great.
> 
> Tim Koop
> 
> 
> 
> Mikolaj Rydzewski wrote:
> 
>> Tim Koop wrote:
>>
>>> I have a web page form that is expecting UTF-8 data, but when my code 
>>> gets the data, the getParameter() methods don't return the right 
>>> thing.  I have to run this code to get the right thing:
>>>
>>> String value = new 
>>> String(request.getParameter("fieldName").getBytes("ISO-8859-1"), 
>>> "UTF-8");
>>
>>
>>
>> Please take a look at SetCharacterEncodingFilter servlet in Tomcat's 
>> example apps. It does the job you need. Its sources are located in 
>> webapps/jsp-examples/WEB-INF/classes/filters (at least in Tomcat 5.5).
>>
>> Regards
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 

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


Re: Request parameter data not being recognised as UTF-8

Posted by Tim Koop <gr...@timkoop.com>.
Thanks Mikolaj.  That did it.

For some reason running request.setCharacterEncoding("UTF-8") in my code 
does nothing, but if it gets run from a Filter it works great.

Tim Koop



Mikolaj Rydzewski wrote:

> Tim Koop wrote:
>
>> I have a web page form that is expecting UTF-8 data, but when my code 
>> gets the data, the getParameter() methods don't return the right 
>> thing.  I have to run this code to get the right thing:
>>
>> String value = new 
>> String(request.getParameter("fieldName").getBytes("ISO-8859-1"), 
>> "UTF-8");
>
>
> Please take a look at SetCharacterEncodingFilter servlet in Tomcat's 
> example apps. It does the job you need. Its sources are located in 
> webapps/jsp-examples/WEB-INF/classes/filters (at least in Tomcat 5.5).
>
> Regards
>

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


Re: Request parameter data not being recognised as UTF-8

Posted by Mikolaj Rydzewski <mi...@becomo.com>.
Tim Koop wrote:

> I have a web page form that is expecting UTF-8 data, but when my code 
> gets the data, the getParameter() methods don't return the right 
> thing.  I have to run this code to get the right thing:
>
> String value = new 
> String(request.getParameter("fieldName").getBytes("ISO-8859-1"), 
> "UTF-8");

Please take a look at SetCharacterEncodingFilter servlet in Tomcat's 
example apps. It does the job you need. Its sources are located in 
webapps/jsp-examples/WEB-INF/classes/filters (at least in Tomcat 5.5).

Regards

-- 
Mikolaj Rydzewski  <mi...@becomo.com>
Becomo S.A.
tel. (12) 2927104



Re: Request parameter data not being recognised as UTF-8

Posted by Tim Koop <gr...@timkoop.com>.
Thanks Mark.

I entered "Tübingen" and I got back "Data posted to this form was: 
Tübingen".  localhost_log also has "Tübingen"

It does this for both port 8080 (Tomcat directly), and port 80 (through 
Apache).

I checked the Tomcat 4.1.31 release notes 
(http://apache.sunsite.ualberta.ca/jakarta/tomcat-4/v4.1.31/RELEASE-NOTES) 
and the closest thing I could find is the following, which is only 
documentation.

[4.1.31] Docs:
         Correctly document default value for useBodyEncodingForURI

I modified the simple jsp page to have these lines:
    <%
      out.print("Char endoding = " + request.getCharacterEncoding() + "<p>");
      request.setCharacterEncoding("utf-8");
      out.print("Data posted to this form was: " + request.getParameter("mydata") + "<p>");
      out.print("Char endoding = " + request.getCharacterEncoding() + "<p>");
    %>

The response was:

Char endoding = null

Data posted to this form was: Tübingen

Char endoding = utf-8


Should I have something in my web.xml file?  Is there some way to verify 
that what I enter in the Connector tag in server.xml is actually 
sticking?  I couldn't find any errors in localhost.log nor in 
cataline.out (well, no relavent errors at least).

Thanks a lot, to anyone who has any ideas.

--
Tim Koop




Mark Thomas wrote:

>
> 1. Try accessing Tomcat directly to see if mod_jk is causing problems.
> 2. Try 4.1.31 - it has a bunch of bug fixes that might (I haven't 
> checked the release note for details) help
> 3. Try the following simple test JSP and once this works, see how your 
> app is different.
>
> <%@ page contentType="text/html; charset=UTF-8" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
>   <head>
>     <title>Encoding fun</title>
>   </head>
>   <body>
>     <p>Data posted to this form was:
>     <%
>       request.setCharacterEncoding("UTF-8");
>       out.print(request.getParameter("mydata"));
>     %>
>
>     </p>
>     <form method="post" action="index.jsp"
>           enctype="application/x-www-form-urlencoded">
>       <input type="text" name="mydata">
>       <input type="submit" value="Submit" />
>       <input type="reset" value="Reset" />
>     </form>
>   </body>
> </html>
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>

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


Re: Request parameter data not being recognised as UTF-8

Posted by Mark Thomas <ma...@apache.org>.
Tim Koop wrote:
> I have a web page form that is expecting UTF-8 data, but when my code 
> gets the data, the getParameter() methods don't return the right thing.  
<snip>
> In server.xml, I have added these lines to each Connector:
>  URIEncoding="UTF-8"  useBodyEncodingForURI="false"
>  (I have also tried useBodyEncodingForURI="true")
Leave this as false.
> I have added this text to JAVA_OPTS (in my rc.d init script) and have 
> confirmed that these are indeed Java system properties
> -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8
I am not sure if the first one of these will do anything and I am 
certain the second will not - it is a read only property on most JVMs.

> I have tried calling request.setCharacterEncoding("UTF-8") in my code.  
> I've even made it the first line of the jsp page.  No luck.
This is good.

> I even have the environment variable LANG set to "en_GB.UTF-8"
Not necessary.

> 
> I have <%@ page contentType="text/html; charset=UTF-8"  
> pageEncoding="UTF-8" %> in my jsp page.
Good.

> I have <meta http-equiv="Content-Type" content="text/html; 
> charset=utf-8"> in my jsp page.
Not necessary.
> Should this actually work and I'm just doing something wrong?
Yes it should (and does for me) work.

> Is there anything else I can try?

1. Try accessing Tomcat directly to see if mod_jk is causing problems.
2. Try 4.1.31 - it has a bunch of bug fixes that might (I haven't 
checked the release note for details) help
3. Try the following simple test JSP and once this works, see how your 
app is different.

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     <title>Encoding fun</title>
   </head>
   <body>
     <p>Data posted to this form was:
     <%
       request.setCharacterEncoding("UTF-8");
       out.print(request.getParameter("mydata"));
     %>

     </p>
     <form method="post" action="index.jsp"
           enctype="application/x-www-form-urlencoded">
       <input type="text" name="mydata">
       <input type="submit" value="Submit" />
       <input type="reset" value="Reset" />
     </form>
   </body>
</html>

Mark


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


Re: Request parameter data not being recognised as UTF-8

Posted by Manfred Steurer <ma...@gamed.com>.
Try saving the JSP-File itself as UTF-8.
Worked for me at a similar Problem.

- ms



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