Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

806
Views
UTF-8 not working in Java Application running in Tomcat server

I am working on a Java application. I need to get UTF-8 encoding in my Java webapp to support Bengali (বাংলা) text. I have done the following:

Tomcat's server.xml

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

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

<Connector protocol="AJP/1.3"
    address="::1"
    port="8009"
    redirectPort="8443"
    URIEncoding="UTF-8" />

JVM defaultCharset in catalina.bat file

set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8

properties in application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/database_name?useUnicode=true\&characterEncoding=UTF-8
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=UTF-8

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

server.tomcat.uri-encoding=UTF-8
spring.webflux.multipart.headers-charset=UTF-8
spring.thymeleaf.encoding=UTF-8

meta tag in html file

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
    <head>
        <meta charset="utf-8">
    </head>

    <body>
    </body>
</html>

utf-8 support in form tag

<form enctype="multipart/form-data" accept-charset="UTF-8" action="#" th:action="@{/create}" th:object="${object}" th:method="POST">
    <div class="form-group">
        <label for="name" class="col-form-label">Name</label>
        <input type="text" class="form-control" id="name" name="name" th:field="*{name}" placeholder="Enter Name">
    </div>
<div class="form-group">
    <label for="photo">Photo</label>
    <input type="file" class="form-control-file" id="photo" name="photo"/>
</div>
    <div>
        <button class="btn" type="submit">Submit</button>
    </div>
</form>

MySQL configuration (my.ini)

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

MySQL properties:

Database:
Default collation: utf8mb4_0900_ai_ci
Default charactterset: utf8mb4

Table:
Table collation: utf8mb4_0900_ai_ci

Column:
Type: varchar(255)
Character Set: utf8mb4
Collation: utf8mb4_0900_ai_ci

Configuration:

  • Java 11.0.2
  • Tomcat 8.5
  • MySQL 8.0.16
  • Spring Boot 2.2.4
  • Maven 3.8.1
  • Windows Server 2019 Standard (Production) + Windows 10 Home (Development)

When I submit a form with value আনোয়ার, it is saved as আনোয়ার

How can I solve this problem?

When I run the application from eclipse it works fine. But when the war file is deployed in Tomcat server it does not work.

I tried the following code. It prints আনোয়ার in tomcat8-stdout file. So I think problem occurs while transferring data from browser to server, from server to database is fine.

@PostMapping("/create")
public String create(@ModelAttribute("object") Object object, @RequestParam("photo") MultipartFile photo) throws IOException {
    System.out.println(object.getName());
    return "redirect:/index";
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The encoding of POST parameters is not set at the Connector level, but on the ServletRequest object.

Tomcat provides a filter to set it, as explained in the documentation.

Add this to your web.xml file:

<filter>
  <filter-name>setCharacterEncodingFilter</filter-name>
  <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
</filter>

<filter-mapping>
  <filter-name>setCharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
over 4 years ago · Santiago Trujillo Report

0

Did you try to set the below in the server.xml for tomcat to allow escaped chars in the URL's

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" relaxedPathChars="[]|" relaxedQueryChars="&#x5B;&#x5D;&#x7C;&#x7B;&#x7D;&#x5E;&#x5C;&#x60;&#x22;&#x3C;&#x3E;" redirectPort="8443" />
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!