1

I have been using Apache Velocity for years in my Tomcat web platform.

I was under very old versions: velocity-1.6.4 and velocity-tools-1.4 and I recently decided to migrate my whole platform and to use velocity-engine 2.3 and velocity-tools 3.1

Since I have around 15 web applications, the process was a bit long but quite straightforward. Unfortunately, I have some rendering problems, specially :

  • The Euro sign € is not rendered properly.
  • When the web page loads external scripts with labels, (accents in French, special characters) they seem to be ANSI characters.

Here is the kind of stuff I get:

Accents, dollar, punctuation: àèù, $, ! ?,

Euro sign: ?

From external js : éà èù $##!?€

The same template, with same tomcat configuration, but with the very old versions of velocity displays perfectly. If plain HTML, it also works (the Connectors in Tomcat config file server.xml precise UTF-8).

Every thing is in UTF-8. The velocityservlet is a basic "velocity servlet", as simple as possible. With the last versions of Velocity, the velocity properties config file only contains:

resource.loaders = webapp

resource.loader.webapp.class = org.apache.velocity.tools.view.WebappResourceLoader

resource.loader.webapp.path = /WEB-INF/templates/

I have checked by code inside the servlet that the property getVelocityProperty(RuntimeConstants.INPUT_ENCODING, RuntimeConstants.ENCODING_DEFAULT) was UTF-8.

After nights and days on the problem, I feel quite desperate! Thank you for your help.

  • You face a [mojibake](https://en.wikipedia.org/wiki/Mojibake) case (*example in Python for its universal intelligibility*): `print( '€éàèù'.encode( 'utf-8').decode( 'cp1252'))` returns `€éàèù` – JosefZ May 03 '23 at 20:45
  • Yes, thank you, but the javascript file is UTF-8 encoded and everything is UTF-8 and if I take old version of Velocity, it is fine. – Dominique Sauquet May 03 '23 at 21:47
  • And if I copy the velocity template in an HTML file, it is perfect. Something must be wrong with my usage of this particular version of Velocity. – Dominique Sauquet May 03 '23 at 21:54
  • 1
    Which kind of view servlet are you using? Where do you set the encoding of the response? – Claude Brisson May 04 '23 at 15:02

2 Answers2

1

I found my problem! My "Charset filter" was not filtering "response" but only request. However, I am puzzled by the code of the method initRequest of class VelocityViewServlet:

protected void initRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { try { request.setCharacterEncoding(getVelocityProperty(RuntimeConstants.INPUT_ENCODING, RuntimeConstants.ENCODING_DEFAULT));

    }
    catch (UnsupportedEncodingException uee)
    {
        error(request, response, uee);
        throw uee;
    }
}

I wonder why Velocity does not apply the setCharacterEncoding on the response. And only on the request. I would have added the response.setCharacterEncoding line, intuitively: response.setCharacterEncoding(getVelocityProperty(RuntimeConstants.INPUT_ENCODING, RuntimeConstants.ENCODING_DEFAULT));

In the old version of Velocity, we had 2 config parameters: input.encoding=UTF-8 output.encoding=UTF-8

And now, only one: resource.default_encoding=UTF-8 But it should be applied to both request and response. Am I missing a point?

0

I had the same trouble today, finally I override the protected setContentType method in my VeloServlet class and the problem was solved:

public class MyVeloServlet extends VelocityViewServlet {

...

    @Override
    protected void setContentType(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
    }
}

It is possible to reach this result with a configuration key in velocity.properties:

default.contentType=text/html; charset=UTF-8
Miklos Krivan
  • 1,732
  • 20
  • 14