Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8281308

Java CGI program in Firefox has default charset US-ASCII, can't print UTF-8

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 20.04.3 LTS
      openjdk 11.0.13 2021-10-19
      OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04)
      OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
      Firefox 96.0
      Apache/2.4.41 (Ubuntu) Server at localhost Port 80


      A DESCRIPTION OF THE PROBLEM :
      Printing System.out.println(stringvar) prints question marks (???) for UTF-8 characters. I'm unable to set the Charset.defaultCharset() to "UTF-8" or StandardCharsets.UTF_8. Even if I type in the bash shell "export JAVA_TOOL_OPTIONS="-Dfile.encoding=UTF-8", this has no effect.

      If I run the same program with the same output in the bash shell, the defaultCharset shows UTF-8 and the letters print correctly, but running it as a CGI program to Firefox prints question marks. If I run the same CGI program in Python, Perl, or Ruby, it works fine, but I cannot get the UTF-8 encoding to work in Java.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      http://localhost/cgia/libmsri/tryit.cgi
      My cgi program contains: System.out.println("<h1>äèëîöœûÿÇÉ</h1>");

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should print äèëîöœûÿÇÉ to the browser web page.
      ACTUAL -
      It prints ?????????? to the browser web page.

      ---------- BEGIN SOURCE ----------
      #!/usr/bin/java --source 11

      import java.util.*;
      import java.io.*;
      import java.util.stream.Collectors;
      import java.net.URLDecoder;
      import java.net.URLEncoder;
      import java.nio.charset.StandardCharsets;
      import java.util.regex.Pattern;
      import java.util.regex.Matcher;
      import java.nio.charset.Charset;
      import java.io.InputStreamReader;
      import java.io.FileInputStream;

      public class Tryit {

          public static void main( String argv[] )
          {

      System.setProperty("file.encoding","UTF-8");
      System.out.println("Content-Type: text/html;charset=UTF-8\r\n\r");
      String header = "<html>\n<head>\n<meta charset=\"UTF-8\">\n";
      header = header + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n";
      header = header + "<title>Try UTF-8 Encoding</title>\n</head>\n<body>";
      System.out.println(header);
      System.out.println("<h1>äèëîöœûÿÇÉ</h1>");
      System.out.println("</body>\n</html>");
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. example-bash.sh
          0.5 kB
          Andrew Wang
        2. Tryit.java
          0.9 kB
          Andrew Wang

            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: