velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mailmur <mail...@yahoo.com>
Subject Re: [HELP] How can I set pageEncoding in Velocity (.vm page)
Date Thu, 22 Jun 2006 11:59:38 GMT
I have own servlet where I handle requests. I try to
create all webapps as UTF-8 enabled, no options
allowed.  Even all config files are stored as
utf-8_with_bom files :-)

I use my UnicodeReader to read all .properties files
and .vm files. I save files as UTF-8_withBOM text
files, Windows Notepad can do it. See this page:
http://koti.mbnet.fi/akini/java/unicodereader/

You may customize velocity provided servlet to force
utf-8 charset. Just take sources and edit servlet you
use.
* first command in doPost/doGet method is
request.setCharacterEncoding("UTF-8")
this must be called before anyone use req.getParameter
methods. You may break this if you have filter
preprocessors and filters dont oboey this rule.

* update Content-Type header for the response
accordingly.
response.setContentType("text/html; charset=UTF-8")
this put webbrowsers display all possible characters
fine. 

* and to play even more safe add the following html
metatag on each html page.
<html>
<head>
  <meta http-equiv="content-type" content="text/html;
charset=UTF-8" />
  <title>MyUTF8Page</title>
</head>
<body>....

* Always give .jsp directives for jsp pages.
you saved .jsp as legacy ISO-8859-1 file:
  <%@ page contentType="text/html; charset=UTF-8" 
     pageEncoding="ISO-8859-1" 
  %>

you saved .jsp as UTF-8 file:
  <%@ page contentType="text/html; charset=UTF-8" 
     pageEncoding="UTF-8"
  %>

And everything should do fine. If I must edit jsp
pages directly through a linux console I usually save
files as ISO-8859-1. Our Pine texteditor does not
support utf8 properly.

* webclients post forms using a container html page
charset. So if you return utf-8 charset enabled web
page then you will get name-value pairs as
UTF8urlencoded values. Unfortunately each and every
webbrowser is a buggy and don't let you know it. 

They should post Content-Type header as:
application/x-www-form-urlencoded; charset=UTF-8

But they only do:
application/x-www-form-urlencoded

If servlet engine (tomcat, ...) does not find charset
attribute from the request it fallbacks to legacy
ISO-8859-1 charset. You then will get wrong
bytesToString conversion.

You must handle it by using mentioned
req.setCharacterEncoding("UTF-8") before reading
parameter values from the request. Then you
automatically get a proper bytesToString conversion
behind the scene.

Believe me, if you must create cross-international
webapps your life is a nightmare without a true
end-to-end UTF8 charset. I create Mysql databases with
default UTF8 charset, save config files as
utf8withbom, http request and responses are forced to
utf8 charset, etc...



thx
--- Pham Anh Tuan <anhtuan@ichi-corp.jp> wrote:

> ah, thanks to Antonius Ng, I create a filter and
> filtering all url-pattern 
> *.do or *.vm :)
> 
> again, thank you :)
> ----- Original Message ----- 
> From: "Antonius Ng" <antonius.ng@n2nconsulting.com>
> To: "Velocity Users List"
> <velocity-user@jakarta.apache.org>
> Sent: Thursday, June 22, 2006 11:08 AM
> Subject: Re: [HELP] How can I set pageEncoding in
> Velocity (.vm page)
> 
> 
> > Hi bowlkhin,
> >
> > Maybe you can add the following to your servlet.
> > req.setCharacterEncoding("UTF-8");
> >
> > I have the above line in my Filter.
> >
> > Cheers,
> > Antonius Ng
> >
> > Pham Anh Tuan wrote:
> >
> >> Thank you, Antonius Ng,
> >>
> >> I did but it give me no effect :|
> >>
> >> I don't know why :(
> >>
> >> only if I add attribute
> enctype="multipart/form-data" in <form> tag, I 
> >> get unicode characters :|
> >> ----- Original Message ----- From: "Antonius Ng" 
> >> <antonius.ng@n2nconsulting.com>
> >> To: "Velocity Users List"
> <velocity-user@jakarta.apache.org>
> >> Sent: Thursday, June 22, 2006 10:46 AM
> >> Subject: Re: [HELP] How can I set pageEncoding in
> Velocity (.vm page)
> >>
> >>
> >>> Hi bowlkhin,
> >>>
> >>> I usually set it at velocity.properties
> >>>
> >>>
>
#----------------------------------------------------------------------------
> >>>
> >>> # T E M P L A T E  E N C O D I N G
> >>>
>
#----------------------------------------------------------------------------
> >>>
> >>>
> >>> input.encoding=UTF-8
> >>> output.encoding=UTF-8
> >>>
> >>> Cheers,
> >>> Antonius Ng
> >>>
> >>> Pham Anh Tuan wrote:
> >>>
> >>>> Hi all,
> >>>>
> >>>> plz help me find out the way to set
> pageEncoding in .vm file :(
> >>>>
> >>>> thanks in advance
> >>>>
> >>>> bowlkhin
> >>>>
> >>>
> >>>
> >>>
> >>>
>
---------------------------------------------------------------------
> >>> To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> >>> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> >>>
> >>>
> >>
> >>
> >>
> >>
>
---------------------------------------------------------------------
> >> To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> >>
> >>
> >
> >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> >
> > 
> 
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Mime
View raw message