Handle Arabic Encoding common Issues in Java Web Development

Introduction

Writing java application that support internationalization web application could be annoying sometimes. Handling English applications is not the same as handling foreign languages such as Arabic. The post is to mention the main steps taken to handle encoding Arabic characters in Java web applications.

We need to accept that UTF-8 is the character set used in Web.

Write Arabic characters in an JSP

When we try to hard-code Arabic characters in an JSP, we can do this in two ways :

  • We can save the JSP file in UTF-8 format, then use UTF-8 compatible editor (Netbeans or JDeveloper) and set the editing language as UTF-8, not forgetting to specify that the characterset of the JSP is UTF-8 in the top of the JSP code : <%@page contentType="text/html" pageEncoding="UTF-8"%>
  • Use any Unicode converter site like “Unicodetools.com” to write the Arabic characters in its Unicode representation. Example : The Arabic word “عربي” is converted into “& #1593;  & #1585;  & #1576; & #1610;” where the code number after the # character is hexadecimal the Unicode of the Character.
  • We can use resource bundle files and make sure that the Arabic strings inside it are saved in Unicode format (Using Netbeans properties file editor) to make sure the string will be compatible with any char-set JSP.

Handle FORM Posting Arabic characters

When developing post action, Any Arabic characters are posted in Latin1 Encoding (8859_1) which are needed to be encoded in UTF-8 to be processed and displayed correctly. It is done like this :

form1.setInputText1(new String(form1.getInputText1().getBytes("8859_1"), "UTF-8"));

Note: the form still holds the data inside it, so the input text bounded to the Form property still displays the same value entered by the user. So, if you are not intending to reset the form, the value entered should be also encoded to be displayed correctly, then set in the form.

Handle Ajax Posting Arabic Characters

Using simple Ajax to post Arabic characters is more straightforward. You need to prepare the Ajax request object, prepare the URL and collect the parameters from the HTML page using Java Script. Then open the request on the specified URL, then send the parameters required. Check the following example :

<script type="text/javascript">
function postAjax()
{
ajaxText1 = document.getElementById("ajaxText1").value;
params = "ajaxText1="+ajaxText1;
url = "postAjaxData.do";

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}

xmlhttp.open(“POST”, url, true);
xmlhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded; charset=utf-8”);
xmlhttp.setRequestHeader(“Content-length”, params.length);
xmlhttp.setRequestHeader(“Connection”, “close”);

xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(“ajaxDiv”).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send(params);
}
</script>

Also you can find a working application that demonstrate these steps, here. The sample is build using Netbeans 6.9 & it demonstrate the following :

  • Building struts application
  • Hard coded Arabic characters in UTF-8 JSP
  • Posting Arabic characters.
  • Using resource bundles in Struts to display Arabic messages.
  • Hibernate loading and saving Arabic characters to Derpy DB.
Advertisements

10 thoughts on “Handle Arabic Encoding common Issues in Java Web Development

  1. Thank you for this topic ,,

    I have an arabic encoding problem, I set the encoding to be UTF-8 as this

    and I use Java Studio Creator with MySql to build the system, when I pass the value from JSC to MySql it shown as question marks ????.

    Can you explain more the ways that you suggest , please

    And Thanks in Advance

  2. Sorry, I didn’t get the issue you are facing. I don’t know JSC and I think it is the old version of Netbeans. Are you passing the Arabic value in java code or what?

    1. Thank you Ahmad for your response, I really appreciate it.
      Java Studio Creator is a web development using jsp. It use regular java statements a drag and drop GUI components. I just solved the problem before few seconds using this solution

      jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8

      Thank you veru much, you know as we said ‘wjhk 5air 3lay’ :).

  3. Mohamed Shawky 21/05/2013 — 11:40 AM

    Dear Ahmed,

    I think this article is very good article thanks for your effort

    1. Thank you Mr Shawky for your comment 🙂

  4. great article 🙂 i have a question if you may: i have encoding issue when handling data in arabic i managed to fix it but still there is an error when handling the “Fa” letter (ف) it appears as question marks, i tried encodings UTF-8 and Windows-1256, any help?

    1. It depends whether you use in Java or JSP. What is the case you are facing ?

      1. i faced the problem while using RESTful web service along with jersey API

      2. Hello Ahmad i am facing this issue in java do you have any solution.??
        Please reply.

  5. Good job done… its helped me lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close