In order to handle Unicode characters correctly in a web-based application, we need to look into these three areas:
Data Source:
First, data source should be able to support Unicode either in file format, database or text entry field.
Browser:
At the browser end, we need to set character encoding and install a font that can display those Unicode characters. Typically, MS Arial font from Microsoft is a possible choice or any other MS Unicode fonts.
Processor:
This is where JSP should produce unicode compliant pages. I think our problem happens right here. What we have to do is modifying charset when writing a jsp page.
Currently, I think we have two possible solutions for this problem:
1) Specifying contentType and character set in JSP page directive:
In JSP page directive, weâll specify contentType with character set we are using in the page. Remember, we have to specify it on the page where Unicode characters exist.
Either the following line
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
or
<%@ page language="java"
contentType="text/html;
charset=utf-8"
pageEncoding="utf-8"
import="java.sql.*"
%>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
would do the job.
This way is somehow cumbersome because we have to specify contentType and charset for each page where we expect to display Unicode characters.
2) Setting CharacterEncoding of HttpServletRequest to support UTF-8
In ActionServlet or any extended AcionServlet class, I think we can modify HttpServletRequest by adding this piece of code
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
try{
request.setCharacterEncoding("utf-8");
}catch(Exception ex){
}
super.process(request,response);
}
Try this and let me know the result.
Good luck,
Tan
|