2

I have an index.html file:

<!doctype html>
<html lang="fr" class="no-js fontawesome-i2svg-active fontawesome-i2svg-complete">
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  <title>Annuaire Téléphonique</title>
...

and i deploy it into a war, served by Tomcat 9.0.35. The problem is that in the browser, the accents are like this: "Annuaire Téléphonique"

I found that the content-type served by Tomcat is false:

Every piece of Tomcat configuration is configured for UTF-8. My Linux bash defines LANG=UTF-8, and Tomcat is started with -Dfile.encoding=UTF-8

I also found that when asking form index.html, tomcat does not specify encoding, which is perfect for me as index.html itself contains the Content-Type meta with UTF-8, the browser display correctly the accents

Questions are:

  • Why index.html served as welcome file (http://127.0.0.1:10000/) is served as ISO-8859-1 ?
  • How can it be served as UTF-8 ? (AddDefaultCharsetFilter UTF-8 doesn't help)

Thanks for your help

Shimbawa
  • 262
  • 3
  • 12
  • Are you sure the selected welcome file is `index.html` (e.g. modify the file)? Which browser are you using? Can you add the configuration of the `AddDefaultCharsetFilter`? – Piotr P. Karwasz Oct 28 '21 at 20:01
  • I modified index.html, it is the file served. I tried with Chrome/Linux and Bing/Windows. Same with curl/localhost. Here is the filter configuration: addDefaultCharsetFilter org.apache.catalina.filters.AddDefaultCharsetFilter encoding UTF-8 – Shimbawa Oct 29 '21 at 06:24
  • Please, edit your question and add the filter configuration there. If you don't have a filter mapping, the filter is not used. – Piotr P. Karwasz Oct 29 '21 at 14:27

2 Answers2

0

I found a working solution as I'm exposing a Spring application, with the following properties

spring.http.encoding.charset=UTF-8 spring.http.encoding.force-response=true

This behaviour is still strange to me...

Shimbawa
  • 262
  • 3
  • 12
0

I had the same problem:

direct access to the url :

  • example.com/index.html (encoding is ok)
  • example.com/ (encoding is wrong) -> text/html;charset=ISO-8859-1

I'm also working with spring-boot.

spring.http.encoding.charset=UTF-8 didn't work for me.

This is my solution :)

@Component
public class CustomFilter implements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        if (request instanceof HttpServletRequest)
        {
            String servletPath = ((HttpServletRequest) request).getServletPath();
            if (servletPath.equals("/")) {
                // do not let tomcat assign  text/html;charset=ISO-8859-1
                response.setContentType("text/html; charset=UTF-8");
            }
        }
        chain.doFilter(request, response);
    }
}
Sam Roy
  • 1
  • 1