Tuesday, January 4, 2022

How to resolve the encoding issue while building Java Application

In this tutorial, we are going to understand the different scenarios where we might get encoding issues while building the Java application as a war or jar file and deploy it to the server. 

The main encoding issue here is, for some languages like Hebrew, Chinese, Arabic, etc there might get the chance of appearing as question marks instead of actual specific text. This issue might be due to different cases.

1. Sometimes the serverside rendering language like jsp, gsp pages might cause the issue we can use the following tag in those pages.

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

2. We might get the issue due to database data where we don't set the configugre the utf-8 encoding. To resolve the issue we can set the jdbc connection url as follows:
jdbc:mysql://hostname/database_naeme?useUnicode=yes&&characterEncoding=UTF-8

3. If our application contains some other language that requires saving operation in the database. Better to create the database using encoding using the following command for the MySQL database.
CREATE DATABASE database_name DEFAULT CHARACTER SET "utf8" COLLATE "utf8_general_ci";
Make sure to change the actual database instead of [database_name].

4. If we are using hibernate then better to configure the settings as
hibernate.connection.charSet=UTF-8
hibernate.connection.characterEncoding=UTF-8
hibbernate.connection.useUnicode=true

5. If we are using build tools like Jenkins for deployment we need to configure the encoding In Jenkins: we can create a global variable and set encoding config as below:



Share:

0 comments:

Blog Archive