Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


1. Introduction

UTF-8 is the most common character encoding used in web applications. It supports all languages currently spoken in the world including Chinese, Korean, and Japanese.

In this article, we demonstrate all configuration needed to ensure UTF-8 in Tomcat.

2. Connector Configuration

A Connector listens for connections on a specific port. We need to make sure that all of our Connectors use UTF-8 to encode requests.

Let's add the parameter URIEncoding=”UTF-8″ to all the Connectors in TOMCAT_ROOT/conf/server.xml:



3. Character Set Filter

After configuring the connector, it's time to force the web application to handle all requests and responses in UTF-8.

Let's define a class named CharacterSetFilter:

public class CharacterSetFilter implements Filter {

    // ...

    public void doFilter(
      ServletRequest request, 
      ServletResponse response, 
      FilterChain next) throws IOException, ServletException {
        response.setContentType("text/html; charset=UTF-8");
        next.doFilter(request, response);

    // ...

We need to add the filter to our application's web.xml so that it's applied to all requests and responses:



4. Server Page Encoding

The other part of our web application we need to configure is Java server pages.

The best way to ensure UTF-8 in server pages is to add this tag at the top of each JSP page:

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

5. HTML Page Encoding

While server page encoding tells JVM how to handle page characters, HTML page encoding tells the browser how to handle page characters.

We should add this <meta> tag in the head section of all HTML pages:

<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

6. MySQL Server Configuration

Now, that our Tomcat is configured, it's time to configure the database.

We assume that a MySQL server is used. The configuration file is named my.ini on Windows and my.cnf on Linux.

We need to find the configuration file, search for these parameters, and edit them accordingly:

default-character-set = utf8mb4

default-character-set = utf8mb4

character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

We need to restart MySQL server for the changes to take effect.

7. MySQL Database Configuration

MySQL server character set configuration is only applied to new databases. We need to migrate old ones manually. This can be easily achieved using a few commands.

For each database:

ALTER DATABASE database_name CHARACTER SET = utf8mb4 
    COLLATE = utf8mb4_unicode_ci;

For each table:

    CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

For each VARCHAR or TEXT column:

ALTER TABLE table_name CHANGE column_name column_name 
    VARCHAR(69) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

If we're passing data with UTF-8 characters in database queries, we need to ensure that any database connection made is UTF-8 encoding compliant.

For JDBC based connection this can be achieved with following connection URL:


8. Conclusion

In this article, we demonstrated how to ensure Tomcat uses the UTF-8 encoding.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


Generic footer banner
Comments are closed on this article!