I develop based on eclipse3.2+j2ee5.0 +tomcat5.09+mysql5.0.
First, the Chinese garbled data in the form POST can be solved.
This type of Chinese data can be filtered in real time through filters. The filters code is as follows:
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter ...{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() ...{
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException ...{
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) ...{
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException ...{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) ...{
return (this.encoding);
}
}
filters configuration (configuration web.xml):
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2. The problem of garbled characters when storing Chinese data into the database
is taken as an example in mysql. Just rewrite the connection string:
jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK
3. Problems with passing parameters through URL and identifying Chinese file names
. Symptoms of the problem: 1. Passing parameters through URL, for example:
http://localhost:81/crjy/admin/articlelist.jsp?levelId=64&levelName =Student Party Building
The value obtained through request.getParameter("levleName") is Ranma.
2. Recognize Chinese file names, for example:
<img src="./pic/Sichuan Map.jpg"> Pictures cannot be displayed.
Solution:
1. If you only want to solve the first problem, it is very simple. Just two lines of code:
String role=request.getParameter("chara");
role=new String(role.getBytes("ISO-8859-1"),"GB2312");
out.println(role);
Because the tomcat server uses the ISO-8859-1 character set by default. But this can only solve the first problem but not the Chinese file name problem.
2. Solve the two problems together. Modify server.xml, find the following statement and add URIEncoding="GB18030", so that the two problems are solved together (no role required) =new String(role.getBytes("ISO-8859-1"),"GB2312"); Conversion, the parameters obtained are normal Chinese)
<Connector acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="81" redirectPort="8443"/>
In addition, many articles introduce that you can add URIEncoding="UTF-8", which can solve the Chinese file name problem, but when you get the parameters passed by the url through String role=request.getParameter("chara");, you get UTF-8 Encoded, it needs to be converted to GB2312 which is troublesome.
The above is my summary of my use. I hope everyone can provide valuable opinions.
http://blog.csdn.net/lijiuu/archive/2007/02/25/1514354.aspx