How to Set Unicode (UTF-8) with Tomcat, Java, MySQL, JDBC, HAProxy?
Monday, November 17, 2014
How to Set Unicode (UTF-8) with Tomcat, Java, MySQL, JDBC, HAProxy ?
Set content-type:charset=UTF-8 :-
One example where we will create a simple page with form to enter Unicode strings and display them. The strings will be saved to MySQL database. We will create UTF-8 database and use JDBC to connect to it from our JSP code.
Step 1:-
Create a Unicode database and a database user using cPanel or command line:-
mysql> create database unicode character set utf8 COLLATE utf8_bin;
mysql> create user unicode@localhost IDENTIFIED BY 'mypass123';
mysql> grant all on unicode.* to unicode@localhost;
mysql> flush privileges;
mysql> use unicode;
mysql> create table person (name text);
mysql> \q
Set content-type:charset=UTF-8 :-
One example where we will create a simple page with form to enter Unicode strings and display them. The strings will be saved to MySQL database. We will create UTF-8 database and use JDBC to connect to it from our JSP code.
Step 1:-
Create a Unicode database and a database user using cPanel or command line:-
mysql> create database unicode character set utf8 COLLATE utf8_bin;
mysql> create user unicode@localhost IDENTIFIED BY 'mypass123';
mysql> grant all on unicode.* to unicode@localhost;
mysql> flush privileges;
mysql> use unicode;
mysql> create table person (name text);
mysql> \q
MySQL Connector/J driver connection string has the form of “jdbc:mysql://[hostname]:[port]/[db_name]“
Step 2:-
now following index.jsp to one of your Tomcat webapps. In our example we put it into ~/apache-tomcat-7.0.5/webapps/ROOT for easy access at root URL of our testing domain. There are a few places in the code where UTF-8 is referenced.
<%@page pageEncoding="UTF-8" language="java" import="java.sql.*"%>
<%@page contentType="text/html;charset=UTF-8"%>
<%
try{
request.setCharacterEncoding("UTF-8");
} catch(Exception e) {}
out.println("encoding is "+request.getCharacterEncoding());
out.println("
testing unicode string is 'Miłością'");
testing unicode string is 'Miłością'");
out.println("
request.getLocale() is "+request.getLocale());
request.getLocale() is "+request.getLocale());
out.println("
response.getLocale() is "+response.getLocale());
response.getLocale() is "+response.getLocale());
String name = (String)request.getParameter("name");
java.sql.Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String jdbc = "jdbc:mysql://localhost/unicode?user=unicode&password=mypass123";
String jdbcutf8 = "&useUnicode=true&characterEncoding=UTF-8";
conn = DriverManager.getConnection(jdbc+jdbcutf8);
String sql = "SELECT name FROM person";
Statement st = conn.createStatement();
if(name != null){
// uncomment below line if URIEncoding="UTF-8" is not set in Connector in server.xml
// name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
// comment out below line if URIEncoding="UTF-8" is not set in Connector in server.xml
name = new String(request.getParameter("name"));
out.println("
parameter sent is "+name);
parameter sent is "+name);
String sqlInsert = "INSERT INTO person SET name='"+name+"'";
out.println("
sqlInsert="+sqlInsert);
sqlInsert="+sqlInsert);
st.execute(sqlInsert);
}
ResultSet rs = st.executeQuery(sql);
out.println("
");
");
while(rs.next()) { out.print( rs.getObject(1) + "
"); }
"); }
conn.close();
} catch( Exception ex ) { ex.printStackTrace( new java.io.PrintWriter(out)); }
%>
You can then access and try the form at your root URL e.g. http://username.jvmhost.net/ or http://username.jvmhost.net:HTTPPORTNUMBER/ if you have an account with JVM Host. By default request parameters and values are ISO-8859-1 encoded so you may need to decode them with
Step 3:-
name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
Step 4:-
Alternatively, you can tell Tomcat to to do the encoding/decoding for you using URIEncoding=”UTF-8″. Below this setting is activated for both HTTP and AJP connectors as both are used by default at JVM Host. You can add URIEncoding in server.xml.
connectionTimeout="20000" redirectPort="10105" URIEncoding="UTF-8" />
Step 5:-
With this setting active you can just use
name = new String(request.getParameter("name"));
This way you can have any international characters hardcoded in your pages display correctly. Also international characters sent to database via the form are read back and displayed correctly.
Set content-type:charset=UTF-8 in HAProxy:-
pls set below line in backend section just before load balancer servers.
http-response set-header Content-Type text/html;charset=UTF-8
Set Vary:User-Agent in HAProxy:-
pls set below line in backend section just before load balancer servers.
http-response set-header Vary User-Agent
0 comments:
Post a Comment