Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Setting text/javascript encoding to utf8 with Jetty 10.0.16

I have problems that my UTF8 file is displayed wrongly in the browser -

  https://wordsbyfarber.com/Consts-ru.js

That is because the document itself is saved in the "windows-1252" charset.
No amount of fiddling with the `Content-Type` will fix that.
You'll have to fix your source document to be UTF-8 first.

Joakim Erdfelt / joakim@xxxxxxxxxxx


On Tue, Oct 3, 2023 at 9:35 AM Alexander Farber via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
Thank you Joakim for the extensive answer, however -

On Tue, Oct 3, 2023 at 2:23 PM Joakim Erdfelt via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
Per the _javascript_ RFC, the `text/_javascript_` mime-type has an optional `charset` parameter.

Essentially, if the charset is unspecified, then the encoding is UTF-8.

> the encoding is unfortunately not set to utf8 (like it is for the served json files).

For JSON, the charset parameter is not used.
Per spec, JSON is always UTF-8.

In Jetty, the json encoding is specified as an assumed UTF-8.
This means the `charset` parameter is not produced when generating the `Content-Type` header, and is ignored when parsing the `Content-Type` header.

 > Is there a way to enforce that without compiling a custom version of Jetty?

You can customize the in-place `MimeTypes` for a context.

servletContextHandler.getMimeTypes().addMimeMapping("txt", "text/_javascript_;charset=UTF-8");
or
webappContext.getMimeTypes().addMimeMapping("txt", "text/_javascript_;charset=UTF-8");

or, If you have a WEB-INF/web.xml in your webapp, you can add a `<mime-mapping>` entry.

  <mime-mapping>
    <extension>js</extension>
    <mime-type>text/_javascript_;charset=UTF-8</mime-type>
  </mime-mapping>

I have problems that my UTF8 file is displayed wrongly in the browser -

  https://wordsbyfarber.com/Consts-ru.js

The browser displays pairs of junk characters instead of cyrillic letters and from my (limited) experience having ";charset=utf8" in the header would help it.

So I have followed your suggestion and have extended the src/main/webapp/WEB-INF/web.xml with:

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="false" 
         version="3.1" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    <servlet>
        <servlet-name>WordsServlet</servlet-name>
        <servlet-class>de.afarber.WordsServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>WordsServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/_javascript_;charset=UTF-8</mime-type>
    </mime-mapping>
</web-app>

Unfortunately the URL still returns the headers without "UTF-8" in there:

HTTP/1.1 200 OK
last-modified: Tue, 03 Oct 2023 09:52:40 GMT
content-type: text/_javascript_
accept-ranges: bytes
vary: Accept-Encoding
content-encoding: gzip
server: Jetty(10.0.16)
connection: close

Best regards
Alex

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top