Hi,
use thefilter above, and I think you will don't have any problems with UTF-8
Environments:
1) You have MySQL 4.x as database server with character set UTF-8
2) You create new Tables with character set UTF-8 (it's default)
3) You have (maybe) Tomcat 5.x
4) You're using JDBC (via DatabaseManager) or JNDI (via DataSource)
for database connection
5) Maybe also you're using a framework like Struts/Tapestry, ....
6) JSP's are view components of your application
Now the solutions:
A) Jakarta-Tomcat (in this case, I use JNDI at Tomcat Version 5.5.9):
---------------------------------------------------------------------
Put the context part within your server.xml file (see Tomcat doku if required):
<Context path="/myApp"
docBase="myApp" debug="0" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_com_dev_db_log."
suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/MyDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="1000"
username="username"
password="password"
url="jdbc:mysql://localhost:3306/MyDB"/>
</Context>
B) The Filter component:
------------------------
public class CharsetFilter implements Filter {
private String encoding;
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
encoding = config.getInitParameter("encoding");
if (encoding == null)
encoding = "UTF-8";
}
public void doFilter
(ServletRequest request, ServletResponse response,
FilterChain next) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(encoding);
// ## IMPORTANT !! Otherwise, it doesn't work! ##
response.setCharacterEncoding(encoding);
next.doFilter(request, response);
}
public void destroy() {
// TODO Auto-generated method stub
;
}
}
B1) Register your Filter class within the web.xml file:
<web-app .......>
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>CharsetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
......
</web-app>
C) Your Java class: For this case, I guess you're using Struts
v1.1 or higher
-----------------------------------------------------------------------
/**
* We will use JNDI-Connection
*/
public class MyClass extends Action {
public ActionForward execute(......) {
try {
InitialContext context = new InitialContext();
DataSource source = (DataSource)
context.lookup("java:comp/env/jdbc/MyDB");
Connection con = source.getConnection();
Statement stmnt = con.createStatement();
ResultSet result = stmnt.executeQuery("SELECT * FROM TABLE");
List yourList = new ArrayList();
if(result != null) {
while(result.next()) {
String s = result.getString("UTF8Column");
yourList.add(s);
}
}
request.setAttribute("yourList",yourList);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
return mapping.findForward("success");
}
}
D) Your JSP page (if the process above was successfull):
-------------------------------------------------------------
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<%@ page import="java.util.*" %>
<% List yourList = (List)request.getAttribute("yourList"); %>
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="cache-control" CONTENT="no-cache"="no-cache">
<meta HTTP-EQUIV="expires" CONTENT="no-cache"="0">
<meta HTTP-EQUIV="keywords"
CONTENT="no-cache"="keyword1,keyword2,keyword3">
<meta HTTP-EQUIV="description" CONTENT="no-cache"="This is my page">
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
<select name="objects" size="1">
<%
if(yourList != null) {
for(int i=0; i<yourList.size(); i++) {
String listObject = (String)yourList.get(i);
out.println("<option>" + listObject + "</option>");
}
}
%>
</select>
</body>
</html>
Hope it helps and sorry for my english.
Cengiz