J
J
Jake Taylor2021-07-12 15:36:56
Java
Jake Taylor, 2021-07-12 15:36:56

Why on the JSP page Russian characters are displayed as krakozyabry?

It happened to me that when internationalizing my web project, Russian characters are displayed as krakozyabry.
What I did:
1. I use properties-files in which I write text in the required language. You need to check that the text in these files is set to UTF-8.

It did not help. Move on.

2. Through Filter set encoding UTF-8 .

Create a filter in which we will set the UTF-8 encoding for request and response :

@WebFilter(filterName = "EncodingFilter", urlPatterns = {"/controller"})
   public class EncodingFilter implements Filter {
       private static final Logger LOGGER = LogManager.getLogger();
       private final static String ENCODING_UTF_8 = "UTF-8";
   
       @Override
       public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
           LOGGER.info("Current encoding for request: {}, for response: {}.", servletRequest.getCharacterEncoding(), servletResponse.getCharacterEncoding());
           HttpServletRequest request = (HttpServletRequest) servletRequest;
           HttpServletResponse response = (HttpServletResponse) servletResponse;
   
           request.setCharacterEncoding(ENCODING_UTF_8);
           response.setCharacterEncoding(ENCODING_UTF_8);
           LOGGER.info("Set encoding for request: {}, for response: {}.", request.getCharacterEncoding(), response.getCharacterEncoding());
   
           filterChain.doFilter(request, response);
       }
   }


But that didn't help either. Move on.

3. For each jsp page, you must also set the encoding to UTF-8.

This can be done in two ways:

1. By writing the settings in the web.xml file:

<!-- Encoding for jsp-pages to UTF-8 -->
      <jsp-config>
          <jsp-property-group>
              <url-pattern>*.jsp</url-pattern>
              <page-encoding>UTF-8</page-encoding>
          </jsp-property-group>
      </jsp-config>


2. Write on each jsp page:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


But that didn't help either. Move on.

4. Then, according to [documentation]( tomcat.apache.org/tomcat-7.0-doc/config/http.html ), I configured `Tomcat` as follows.

Open the C:\Program Files\Apache Software Foundation\Tomcat 10.0\conf\server.xml file and add the URIEncoding="UTF-8" attribute to the section . The final section will look like this:

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


But that didn't help either. I rebooted the laptop, I thought it would help, but it did not work. Again we go further.

5. Add this to the jsp page:

<%request.setCharacterEncoding("UTF-8");%>

At the output I get:
ÐлавнаÑ
РнаÑ
ÐонÑакÑÑ


And that doesn't work either. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-07-12
Hasanly @azerphoenix

Good afternoon!

I use properties files in which I write text in the desired language. You need to check that the text in these files is set to UTF-8

If I'm not mistaken, then properties uses ISO 8859-1 encoding
Here, a similar question regarding the encoding of the properties file
https://stackoverflow.com/questions/37436927/utf-8...
As for your quarks, based on this infographic
https ://evc27.pcloud.com/dpZCsmjtXZHvQPG0Z4gx7Z7Zj...
You have UTF-8 encoding, which is displayed in win-1252
Also check your browser's encoding. Some browsers have the ability to set the encoding. If the page is displayed correctly in other browsers and the problem is only in your browser, then most likely there is a problem on the client side with the browser encoding.
And another option in html should be utf-8 encoding inside the head tag
<head>
  <meta charset="UTF-8">
</head>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question