Friday, 22 June 2012

Enabling Mutli-lingual in J2EE web application


Steps to enable Multi-lingual Entry in J2EE web application:

In jsp page:
            add a metadata property in page header
           
                                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                                   
            add a page scriplet
           
                                    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
                                   
            Setting the Character Encoding in request / response Object (Optional)
           
                                    <%request.setCharacterEncoding("UTF-8");
                                      response.setCharacterEncoding("UTF-8");%>
                                     
Do not encode the queryString, use URLEncoder.encode() only for value which is a multi-lingual.
                                    URLEncoder.encode(value,"UTF-8");
                                     
While constructing a request url by ajax,

                                    http_request = new XMLHttpRequest();
                                    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                                    http_request.setRequestHeader("Content-Encoding", "UTF-8");
                                   
While constructing a request url by HttpURLConnection/URL:

                                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                    conn.setRequestProperty("Content-Encoding", "UTF-8");
                                   
In the servlet / action class, before getting the values, set the characterEncoding in request/response Objects.
                                   
Make sure the values which you encode with "UTF-8" must be decoded in the servlet/action class before performing any action on the value.
                                    URLDecoder.decode(value,"UTF-8");
                                   
In JBoss/Tomcat Server, locate the server.xml ( In JBoss, server.xml will be in /server/default/deploy/jbossweb.sar/ )
Add new Attribute "URIEncoding" in the Connector Tag with the value "UTF-8".

                                    <Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}"
               connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />

In Database,
I'll give the changes for Oracle / Mysql

Oracle:
            Change the character set to "al32ut8" which supports multi-lingual.
           
Mysql:
            Default Character set for Mysql 5.1 is latin1.
            Change it to utf8, then Mysql is ready for Multi-lingual characters.
           
            I'll share some query to find the character set and setting those parameter.
            show variables like '%Char%';   // to find the character set of the mysql
            show variables like '%coll%';   // to find the collation the mysql
           
            set @@global.character_set_database='utf8';  // to set utf8 to character_set_database
           
            or change the my.cnf as below
           
            [mysqld]
            character_set_client=utf8
            character-set-server=utf8
           
            [client]
            default-character-set=utf8
           
            Restart the Mysql server "/etc/rc.d/init.d/mysql restart"
            Note: In mysql character set can be set in schema-level, database-level, table-level and 
                       column-level also. Make use of this feature, if applicable.

Your application is now ready for multi-lingual entry............! :-)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Recent Posts

Micro VMs & Unikernels

This post is a follow up of this post . In Previous Post, we discussed about Virtual Machines & Containers architecture. In this post, w...

Older Posts