简体   繁体   中英

Tomcat 9 welcome file encoding is wrong

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>
...

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...

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM