1、MySQL库、表存储设置UTF-8
2、驱动连接字符串String url=
”jdbc:mysql://127.0.0.1:3306/dbname?useUnicode=true&characterEncoding=UTF-8“
3、JSP页面中<%@ page contentType="text/html; charset=utf-8"%>
这样应该就可以传递正确编码了、不过你的输入输出环节的转换代码具体是什么样的?
此前如果有遗漏可能还要检查:
4、设置Tomcat6的server.xml中<Connector>标签:<Connector port="8080"protocol="HTTP/1.1" connectionTimeout="20000"
URIEncoding="utf-8" useBodyEncodingForURI="true" redirectPort="8443" />
5、JSP页面读取后台中文变量<%=new string(aStrVal.getstring(2).getbytes("iso8859-1"),"utf-8")%>
控制装饰层从前端接收中文变量String aStrVal =new String(req.getParameter("strPara").getBytes("ISO8859_1"));
6、如果第4个措施因特殊情况不能修改,则可以自己写过滤器:
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 {
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = "UTF-8";
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
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);
}
chain.doFilter(request, response);
}
/**
* Place this filter into service.
* @param filterConfig The filter configuration object
*/
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);
}
}<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
另外请确认如下都已经设置好:
MySQL 5.1在在my.ini内的 [mysql] 和[mysqld]两处 default-character-set=utf8
MySQL 5.5在在my.ini内的 [mysql]default-character-set=utf8;[mysqld]character-set-server=utf8
Myeclipse偏好首选设置里:
General->Workspace Text file encoding;
General->Content Types,右侧Content Types树,点开Text,确认Java Source File下面的Default encoding框中是UTF-8,用到jsp、js、css同上;
--看你的截图只是数据前后交换的问题,项目工作空间的都没问题了。