Working on an internationalized web application I realized that Glassfish 4.0 is still using ISO 8859-1 encoding to serve web resources. Too bad, as UTF-8 is the de facto standard for encoding on the web.
This short post shows my recipe to make glassfish ready for non latin1 languages.
Add a filter with the following lines of code
as the first filter in your web application filter chain.
-Dfile.encoding=UTF8 to JVM properties in case you load resources from disk.
Fix Request Encoding
Explicitly set the encoding to UTF-8 before request body and or parameters are accessed:
Alternatively, add the following line to the
WEB-INF/glassfish-web.xml of a web application:
<glassfish-web-app> <parameter-encoding default-charset="UTF-8" /> </glassfish-web-app>
Fix response encoding
Response encoding cannot be externally configured. Instead it is always derived from current locale programmatically configured character encoding.
Explicitly set encoding before response writer is accessed:
Otherwise, Glassfish will try to determine encoding from current locale which will always result in inappropriate ASCII derivates.
Fix loading of web resources
In case you are loading files from disk (i.e. from exploded web application archives), make sure to force Glassfish to load these files as UTF-8 by specifying the JVM property