1. 數據庫亂碼
使用EMS SQL Manager for MySQL可以容易地解決這個問題,在table的屬性對話框中將Character Set設為GBK即可。
2. Ajax應用中Servlet返回內容亂碼
設置響應報頭的編碼即可
response.setHeader("Content-Type", "text/html;charset=GB2312");
3. JSP頁面顯示亂碼
在頁面開頭加上
<%@ page pageEncoding="GBK"%>
4. request.getParameter產生亂碼
用post方法只要在獲取參數前設置編碼即可
request.setCharacterEncoding("GBK");
get方法還需要在獲取參數后轉換編碼
name = new String(name.getBytes("ISO8859-1"), "GBK");
5. google到的另一個解決方法,不過貌似不成功 -,-
package filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

public class EncodingFilter implements Filter {
protected FilterConfig filterConfig;

protected String encodingName;

protected boolean enable;

public EncodingFilter() {
this.encodingName = "UTF-8";
this.enable = false;
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
loadConfigParams();
}

private void loadConfigParams() {
this.encodingName = this.filterConfig.getInitParameter("encoding");
String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
if (strIgnoreFlag.equalsIgnoreCase("true")) {
this.enable = true;
} else {
this.enable = false;
}
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (this.enable) {
request.setCharacterEncoding(this.encodingName);
}
chain.doFilter(request, response);
}

public void destroy() {
}
}
web.xml
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/manager/*</url-pattern>
</filter-mapping>
使用EMS SQL Manager for MySQL可以容易地解決這個問題,在table的屬性對話框中將Character Set設為GBK即可。
2. Ajax應用中Servlet返回內容亂碼
設置響應報頭的編碼即可
response.setHeader("Content-Type", "text/html;charset=GB2312");
3. JSP頁面顯示亂碼
在頁面開頭加上
<%@ page pageEncoding="GBK"%>
4. request.getParameter產生亂碼
用post方法只要在獲取參數前設置編碼即可
request.setCharacterEncoding("GBK");
get方法還需要在獲取參數后轉換編碼
name = new String(name.getBytes("ISO8859-1"), "GBK");
5. google到的另一個解決方法,不過貌似不成功 -,-




































































