You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Katrin Grunwald <kg...@cs.tu-berlin.de> on 2004/11/03 17:08:32 UTC

Problem with setting charset in multipart form page

Hello there,

i have a velocity page containing a multipart form. Then there's the 
problem, that certain characters were not retrieved correctly. Well, I 
found a workaround and converted the strings to UTF-8 encoding in the 
Java backend using:

String result = new String(original.getBytes(), "UTF-8");

Still, there was the problem, that those characters were not displayed 
correctly in the browser, whereas everything was fine when debugging 
them from Java. I found the hint to put the following entries to the 
velocity.properties:

input.encoding=UTF-8
output.encoding=UTF-8
default.contentType=text/html; charset=utf-8

With these changes, the data in the multipart form page is displayed 
correctly. But now, those forms, that are no multipart forms, cannot 
display the special characters correctly. Within a JSP-File, everything 
was fine by inserting the

<%@ page language="java" contentType="text/html; charset=utf-8" %> 

directive on the top of the multipart form page. Now I wonder, whether there is anything equivalent in VTL!??? 
The page's header already contains the

<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

directive. But that doesn't work obviously.

I have the impression, I've already searched the whole web but couldn't find the problem's solution. So, it'll be great,
if someone could help me. I hope, I could describe the problem in a way someone does understand it.

Thanks in advance,
Katrin 



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


Re: Problem with setting charset in multipart form page

Posted by Katrin Grunwald <kg...@cs.tu-berlin.de>.
Finally, I solved the problem. The solution is integrating a 
CharacterEncodingFilter as a filter in the application's web.xml.

1. Add "input.encoding=UTF-8" and "output.encoding=UTF-8" to the 
velocity.properties file.
2. Download mmbase.jar from http://www.mmbase.org and insert it into 
WEB-INF/lib.
3. Add the following to the application's web.xml:

  <filter>
    <filter-name>Set Character Encoding</filter-name>
    
<filter-class>org.mmbase.servlet.CharacterEncodingFilter</filter-class>  
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>   
  </filter>

4. In the Java backend, any UTF-8 converting like "new 
String(original.getBytes(), "UTF-8");" must be removed.

I must admit, that I didn't really locate the exact cause of the 
problem. Anyway, I'm happy to have found this workaround.
I hope it will help in case someone should have the same problem.

>
>There can be components whose behavior is possibly buggy (e.g. some of
>the early Jetty 5 betas or alphas had bugs regarding input charsets, or
>MySQL JDBC can be absolutely mad), so it would be important to cleanly
>identify with some carefully chosen tests what charset is actually used
>in the *individual* steps... what does the browser believe about the
>charset (which influences the charset used for sending the form field
>values), the ServletRequest, and the ServletResponse... and, what Writer
>does Velocity use anyway. And, if a database is involved as well, then
>that's yet another component with at least two more points of failure
>(the JDBC connection, and the actual storage).
>
>  
>



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


Re: Problem with setting charset in multipart form page

Posted by Daniel Dekany <dd...@freemail.hu>.
Thursday, November 4, 2004, 11:44:47 AM, Katrin Grunwald wrote:

> Thanks for the tip, unfortunately it didn't work.
> I use Velocity in combination with Tomcat and Struts. I created a class 
> extending the VelocityViewServlet class, that comes along with the 
> Velocity jars. In that subclass, I have set the encoding constants from 
> "ISO-8859-1" to "UTF-8".
> Additionaly, I set the request's and response's character encoding to 
> UTF-8 (in doPost(), as well as in doGet()).
> It doesn't change anything...

There can be components whose behavior is possibly buggy (e.g. some of
the early Jetty 5 betas or alphas had bugs regarding input charsets, or
MySQL JDBC can be absolutely mad), so it would be important to cleanly
identify with some carefully chosen tests what charset is actually used
in the *individual* steps... what does the browser believe about the
charset (which influences the charset used for sending the form field
values), the ServletRequest, and the ServletResponse... and, what Writer
does Velocity use anyway. And, if a database is involved as well, then
that's yet another component with at least two more points of failure
(the JDBC connection, and the actual storage).

-- 
Best regards,
 Daniel Dekany


____________________________________________________________________
Miert fizetsz az internetert? Korlatlan, ingyenes internet hozzaferes a FreeStarttol.
Probald ki most! http://www.freestart.hu

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


Re: Problem with setting charset in multipart form page

Posted by Katrin Grunwald <kg...@cs.tu-berlin.de>.
Thanks for the tip, unfortunately it didn't work.
I use Velocity in combination with Tomcat and Struts. I created a class 
extending the VelocityViewServlet class, that comes along with the 
Velocity jars. In that subclass, I have set the encoding constants from 
"ISO-8859-1" to "UTF-8".
Additionaly, I set the request's and response's character encoding to 
UTF-8 (in doPost(), as well as in doGet()).
It doesn't change anything...

Any further ideas?

Daniel Dekany schrieb:

>Just a tip: You also should set the encoding of the ServletRequest, if
>you didn't. If that's not set explicitly to UTF-8, then maybe it
>defaults to ISO-8859-1, and thus the values that come with the submitted
>form will be decoded incorrectly (depends on the servlet container...).
>Also, it must be done before any HTTP parameters are accessed. (It's
>maybe a good idea to look at the servlet that you are using for invoking
>Velocity to see if what does it set...) All of this is Velocity
>independent, of course.
>
>  
>



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


Re: Problem with setting charset in multipart form page

Posted by Daniel Dekany <dd...@freemail.hu>.
Just a tip: You also should set the encoding of the ServletRequest, if
you didn't. If that's not set explicitly to UTF-8, then maybe it
defaults to ISO-8859-1, and thus the values that come with the submitted
form will be decoded incorrectly (depends on the servlet container...).
Also, it must be done before any HTTP parameters are accessed. (It's
maybe a good idea to look at the servlet that you are using for invoking
Velocity to see if what does it set...) All of this is Velocity
independent, of course.

-- 
Best regards,
 Daniel Dekany


____________________________________________________________________
Miert fizetsz az internetert? Korlatlan, ingyenes internet hozzaferes a FreeStarttol.
Probald ki most! http://www.freestart.hu

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