ASP源码
PHP源码
.NET源码
JSP源码
我开发基于 ec***se3.2+j2ee5.0 +to***t5.09+mysql5.0
一、表单POST的数据的中文乱码解决
这类型的数据中文可以通过filters来实时过滤。filters代码如下:
package filters;
import ja***io.IOException;
import ja***.servlet.Filter;
import ja***.servlet.FilterChain;
import ja***.servlet.FilterConfig;
import ja***.servlet.ServletException;
import ja***.servlet.ServletRequest;
import ja***.servlet.ServletResponse;
import ja***.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter ...{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() ...{
th***encoding = null;
th***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 || (re***st.getCharacterEncoding() == null)) ...{
String encoding = selectEncoding(request);
if (encoding != null)
re***st.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
ch***.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException ...{
th***filterConfig = filterConfig;
th***encoding = fi***rConfig.getInitParameter("encoding");
String value = fi***rConfig.getInitParameter("ignore");
if (value == null)
th***ignore = true;
else if (va***.equalsIgnoreCase("true"))
th***ignore = true;
else if (va***.equalsIgnoreCase("yes"))
th***ignore = true;
else
th***ignore = false;
}
protected String selectEncoding(ServletRequest request) ...{
return (th***encoding);
}
}
filters配置(配置web.xml):
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>fi***rs.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>
二、将中文数据存入数据库乱码问题
以mysql为例,改写连接字符串即可:
jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK
三、 通过url传递参数和识别中文文件名问题
问题表现:1、通过url传递参数,例如:
http://localhost:81/crjy/admin/articlelist.jsp?levelId=64&levelName=学生党建
通过re***st.getParameter("levleName")得到的值为乱玛。
2、识别中文文件名,例如:
图片不能显示。
解决之道:
1、如果只想解决第一个问题那很简单,两句代码即可:
String role=re***st.getParameter("chara");
role=new String(ro***getBytes("ISO-8859-1"),"GB2312");
ou***rintln(role);
因为tomcat服务器默认用 ISO-8859-1 字符集的。但是这样只能解决第一个问题不能解决中文文件名问题
2、两个问题一起解决,修改se***r.xml,找到下列语句添加URIEncoding="GB18030",这样两个问题就一起解决了(不需要role=new String(ro***getBytes("ISO-8859-1"),"GB2312");转化,得到的参数即为正常的中文)
另外许多文章介绍可以添URIEncoding="UTF-8",这样是可以解决中文文件名问题,但是通过String role=re***st.getParameter("chara");得到url传递的参数时,得到的是UTF-8编码的,需要转为GB2312比较麻烦。
以上是本人在使用中的总结,希望大家提供宝贵意见。
http://blog.csdn.net/lijiuu/archive/2007/02/25/1514354.aspx