The essence of the problem lies in the fact that when transmitting text data containing Russian letters, on the side of the servlet I see "???????? ???? ??????"

And now more.

I am building a project in IDEA, I use Ubuntu Desktop 14.04 as OS, I run it on a local TOMCAT. The project is launched, and the data via the post request is transmitted to the servlet - everything works fine.

I copy the assembled project to the remote ubuntu server 14.04 and run it on the local TOMCAT server, everything works as it should except for one problem - when transferring text data containing Russian letters with a post request on the server side, I see ???????? ???? ????? ???????? ???? ?????

What I use:

TOMCAT 8.0.33

java version "1.8.0_91" Java (TM) SE Runtime Environment (build 1.8.0_91-b14) Java HotSpot (TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

Here is such a JSP

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!doctype html> <html lang="ru" class="no-js"> <head> <meta charset="UTF-8"> </head> <body> <form action="regme" class="mt" method="post"> <input type="text" name="fio" placeholder="ФИО" class="form-control mb"> <input type="email" name="email" placeholder="email" class="form-control mb" required> <button class="btn btn-primary btn-block" type="submit">submit</button> </form> </body> </html> 

Here is such a servlet

 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String fio = req.getParameter("fio"); String email = req.getParameter("email"); System.out.println(fio); System.out.println(email); System.out.println(req.getCharacterEncoding()); System.out.println(resp.getCharacterEncoding()); 

When you try to enter the value of wqe@qwe.ru, the following conclusion

 ?????? qwe@qwe.ru UTF-8 UTF-8 

Encoding java files in IDEA UTF-8

What I tried to do:

I tried to change \ add new locales to the system and now it looks like this

 locale -a C C.UTF-8 en_AG en_AG.utf8 en_AU.utf8 en_BW.utf8 en_CA.utf8 en_DK.utf8 en_GB.utf8 en_HK.utf8 en_IE.utf8 en_IN en_IN.utf8 en_NG en_NG.utf8 en_NZ.utf8 en_PH.utf8 en_SG.utf8 en_US.utf8 en_ZA.utf8 en_ZM en_ZM.utf8 en_ZW.utf8 POSIX ru_RU ru_RU.cp1251 ru_RU.iso88595 ru_RU.koi8r ru_RU.utf8 ru_UA.utf8 locale LANG=ru_RU.UTF-8 LANGUAGE= LC_CTYPE="ru_RU.UTF-8" LC_NUMERIC="ru_RU.UTF-8" LC_TIME="ru_RU.UTF-8" LC_COLLATE="ru_RU.UTF-8" LC_MONETARY="ru_RU.UTF-8" LC_MESSAGES=POSIX LC_PAPER="ru_RU.UTF-8" LC_NAME="ru_RU.UTF-8" LC_ADDRESS="ru_RU.UTF-8" LC_TELEPHONE="ru_RU.UTF-8" LC_MEASUREMENT="ru_RU.UTF-8" LC_IDENTIFICATION="ru_RU.UTF-8" LC_ALL= 

I tried to install a filter

 package xxx.filters; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter("/CharEncodingFilter") public class CharEncodingFilter implements Filter { private static final String FILTERABLE_CONTENT_TYPE="application/x-www-form-urlencoded"; private static final String ENCODING_DEFAULT = "UTF-8"; private static final String ENCODING_INIT_PARAM_NAME = "encoding"; private String encoding; @Override public void init(FilterConfig config) throws ServletException{ encoding = config.getInitParameter(ENCODING_INIT_PARAM_NAME); if (encoding == null) encoding = ENCODING_DEFAULT; } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException{ String contentType = req.getContentType(); if (contentType != null && contentType.startsWith(FILTERABLE_CONTENT_TYPE)) req.setCharacterEncoding(encoding); resp.setCharacterEncoding(encoding); chain.doFilter(req, resp); } @Override public void destroy() { } } 

And add this filter to the project's web.xml .

 <filter> <filter-name>CharEncodingFilter</filter-name> <filter-class>orion.filters.CharEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

I tried to edit the server.xml Tomcat server and now it looks like this

 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" useBodyEncodingForURI="true" redirectPort="8443"/> 

I would be grateful for any help.

  • And if fio does not print'om output, and back to the page, also question? - Sergey Gornostaev
  • If fio is displayed back to the page, then everything is ok out.println ("<font color = green>" + fio + "</ font>"); - Petr Khanov
  • So with Tomcat, everything is in order, the problem is with the output to the console. Try new PrintStream(System.out, true, "UTF-8").println(fio); - Sergey Gornostaev
  • PrintStream (System.out, true, "UTF-8"). Println (fio); Displays normally, but System.out.println("йцуйцу"); outputs ?????? - Petr Khanov

2 answers 2

Tomcat is fine, problem with output to console. For correct output you can use

 PrintStream ps = new PrintStream(System.out, true, "utf-8"); ps.println(fio) 

If you want to use System.out.println() , then you can try this

 public class SomeServlet extends HttpServlet { public void init() throws ServletException { try { PrintStream ps = new PrintStream(System.out, true, "utf-8"); System.setOut(ps); } catch(UnsupportedEncodingException e) { ... } } } 

Better yet, do not output to the console from the servlet.

  • The problem is that the data that I submit to the servlet I then mail by javax.mail. *; String subject = "Регистрация" + fio; String text = "Регистрация нового пользователя \nФИО = " + fio + "\nEmail = " + email; String to = "***@*****.****"; EmailSender emailSender = new EmailSender(); emailSender.sendEmail(to, subject, text); And the letters that come to me look in the form of ????????????? - Petr Khanov
  • one
    So, you must explicitly specify the encoding for the letter. - Sergey Gornostaev

The problem was in Tomcat

On the ubuntu server side, Tomcat took ANSI_X3.4-1968 encoding .class files.

 System.out.println("File Encoding = " + System.getProperty("file.encoding")); File Encoding ANSI_X3.4-1968 

On the ubuntu desctop side, Tomcat perceived UTF-8 encoded .class files

 System.out.println("File Encoding = " + System.getProperty("file.encoding")); File Encoding UTF-8 

I do not understand how this is possible

The solution is as follows: You must edit catalina.sh

 sudo nano /opt/tomcat/bin/catalina.sh 

And set the encoding hands. After a large system comment, add a line.

 export CATALINA_OPTS="$CATALINA_OPTS -Dfile.encoding=UTF-8"