JSP中的中文亂碼問題
在Java開發中,中文亂碼是一個最讓人頭疼的問題,如果不對中文做特殊的編碼處理,這些中文字符就會變成亂碼或者是問號。而在不同情況下對這些亂碼的處理方法又各不相同,這就導致很多初學者對中文亂碼問題束手無策。其實造成這種問題的根本原因是Java中才用的默認編碼方式是Unicode,而中文的編碼方式一般情況是GB2312(擴展后為GBK),因為編碼格式的不同,導致在中文不能正常顯示。
常見的中文支持格式
以下是三種支持中文顯示的編碼方案
- UTF-8
- GBK
- GB2312
JSP頁面中文亂碼
在JSP頁面中,中文顯示亂碼有兩種情況:一種是HTML中的中文亂碼,另一中是在JSP中動態輸出的中文亂碼。
造成這種原因的可能就是出在瀏覽器端的字符顯示設置上,我們需要對其進行改進。
<%-- 改造前 --%>
<%@ page language="java" import="java.util.*" %>
<%-- 改造后(也可以用GBK或GB2312) --%>
<%@ page language="java" import="java.util.*" contentType="text/html";charset="utf-8"%>
表單提交中文亂碼
對于表單中提交的數據,可以使用request.getParameter("")
的方法獲取。但是當表單中如果出現中文數據的時候就會出現亂碼。
造成這個問題的原因是:在Tomcat中,對于以POST方法提交的表單采用的默認編碼為ISO-8859-1,而這種編碼格式不支持中文字符。
用utf-8編碼構造一個新字符串
new String(userName.getBytes("ISO-8859-1","utf-8"));
URL傳遞參數中文亂碼
在一般情況下,我們可以采用類似http://localhost:8080/JSPWeb/URLCharset.jsp?param='中文'這種形式來傳遞參數。但是這種傳遞方式仍然有可能會發生亂碼問題。
對于URL傳遞中文參數亂碼這個問題,其處理方法比較獨特,僅僅轉換這個中文字符串的編碼,或者設施JSP頁面顯示編碼都是不能解決問題的。在這里需要多Tomcat服務器的配置文件進行修改才可以解決問題。在這里需要修改Tomcat的conf目錄下的server.xml配置文件。修改方法是在port="8080"后面添加URI編碼設置URIEncoding="gb2312"即可。
<!-- 我是這么設置的 -->
<Connector port="8080"
URIEncoding="utf-8"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
idea輸出亂碼
例如這樣
System.out.println("你好");
- 首先看看字符集是不是設置的支持中文,我一般用utf-8,并且把所有的設置都改成utf-8了
- 目錄下有個
.idea
文件夾,下面有個encodings.xml
,打開,除了utf-8,全都刪掉就好了
<!--就留一個utf-8-->
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" native2AsciiForPropertiesFiles="true" defaultCharsetForPropertiesFiles="UTF-8">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
字符過濾器
package com.springapp.mvc.filter;
import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* Created by TaoYuan on 2017/4/26 0026.
*/
public class EncodingFilter implements Filter {
private String encoding;
private Map<String, String> params = new HashMap<String, String>();
public void init(FilterConfig config) throws ServletException {
System.out.println("begin do the encoding filter!");
encoding = config.getInitParameter("encoding");
for (Enumeration e = config.getInitParameterNames(); e
.hasMoreElements();) {
String name = (String) e.nextElement();
String value = config.getInitParameter(name);
params.put(name, value);
}
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
System.out.println("before encoding " + encoding + " filter!");
req.setCharacterEncoding(encoding);
resp.setContentType("text/html;charset=" + encoding);
chain.doFilter(req, resp);
}
public void destroy() {
params=null;
encoding=null;
}
}
-
web.xml
<filter> <filter-name>setCharacterEncoding</filter-name> <filter-class>com.springapp.mvc.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>