I am doing a chat project
in java
with Spring 3.x
which needs Multi-language support
.
Here is what I have done.
My JSP has:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
My web.xml
has:
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In my Tomcat server.xml
has:
<Connector (...) URIEncoding="UTF-8" />
My my Java environment
has:
JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8
In my Spring-controller
has:
@RequestMapping(value="sendMessage.html",method=RequestMethod.POST)
public @ResponseBody String sendMessage(HttpSession session,@RequestParam String intxnId,@RequestParam String message, HttpServletRequest request,HttpServletResponse response){
String contentType= "text/html;charset=UTF-8";
response.setContentType(contentType);
//response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json; charset=UTF-8");
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("text/plain;charset=UTF-8");
System.out.println("Send Message UTF-8 ----------------- "+ message);
String json = null;
BasicChatProtocol protocol = CustomerManagement.protocol.put(intxnId, chatProtocol.getProtocol());
HashMap<String,String> result = send.send(message, intxnId, protocol);
result.put("name",(String) session.getAttribute("nickName"));
ObjectMapper map = new ObjectMapper();
if(result.size()!= 0){
try {
json = map.writeValueAsString(result);
result.clear();
result = null;
System.out.println("Send Message :::::::: : "+json);
} catch (Exception e) {
e.printStackTrace();
}
}
return json;
}
My jQuery-AJAX
will be:
function sendMessage(){
var intxnId = $("#hide").val();
var message = $("#message").val();
alert("send : \n intxnId : "+intxnId+"\nmessage : "+message);
$.ajax({
type: "POST",
cache: false,
url: contexPath + "/sendMessage.html",
async: true,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
scriptCharset: "utf-8",
dataType: 'html',
data: "intxnId=" + intxnId +"&message="+ encodeURIComponent(message),
success: function(response){
if(response != null && response !="" && response !="null"){
var txt = '{"data":['+response+']}';
var json = eval ("(" + txt + ")");
for(i =0;i<json.data.length;i++){
var data = json.data[i];
var name = data.name;
var message = data.message;
var time = data.time;
alert("Name : "+name+"\nMessage : "+message+"\ntime : "+time);
var createHTML = send(name,message,time);
$("#messageDisplayArea").append(createcreateHTML);
};
}
},
error: function(e){
alert('Error: ' + e);
},
});
}
But when I send the local language message அவர்களுக்கு
(Tamil language), I got only ???????
in the alert box
and the view page
.
But I got the local language
in the console(SysOut in controller
) and all Special Characters
works.
Note : I think I am having the problem with response from the controller
.Because when I send the message
to the controller
I got the message as small boxes
in javascript alert
. But when the response
came I am getting the ?????
in the alert box
.
My console prints,
Send Message UTF-8 ----------------- அவர்களுக்கு
Mesge what I send :::: அவர்களுக்கு
Send Message :::::::: : {"message":"அவர்களுக்கு","time":"time","name":"Human"}
I don't know what I am missing.
Note : I am not using any data base
.
Hope our stack users will give a better solution. Good answers are definitely appreciated.