中文亂碼問題分析
中文亂碼問題對於初學者是一件很頭疼的問題,今天來做個小結。
1、首先確保jsp頁面和tomcat編碼正確
1.1、jsp頁面配置:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
1.2、tomcat配置:
預設使用ISO-8859-1,通常使用時,在apache-tomcat-6.0.16\conf 下的servlet.xml下的connetor屬性中增加URIEncoding
<Server port="8005" shutdown="SHUTDOWN">
<!-- 其他配置 -->
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />
<!-- 其他配置 -->
</Service>
</Server>
2、post請求亂碼和get請求亂碼
注:post請求亂碼和get請求亂碼是不一樣的,get不可以設定請求編碼,post可以設定
2.1、get請求亂碼
當客戶端通過get請求傳送資料時,客戶端無論是gbk還是utf-8編碼,伺服器端都認為是iso-8859-1編碼,也就是說我們通過request.getParameter()獲取資料後,再轉發成正確的編碼.【tomcat6預設是iso-8859-1,tomcat8預設是utf-8】
String username=req.getParameter("username");
username=new String(username.getBytes("iso-8859-1"),"utf-8");
##頁面
<body>
<form action="encoding" method="get">
<input type="text" name="username" value="湯姆"><br>
<input type="text" name="password" value="123456">
<input type="submit" value="submit">
</form>
</body>
## servlet
public class CharacterEncoding extends HttpServlet{ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username=req.getParameter("username"); String password=req.getParameter("password"); System.out.println("username編碼之前:"+username+" password:"+password); username=new String(username.getBytes("iso-8859-1"),"utf-8"); System.out.println("username編碼之後:"+username+" password:"+password); } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
##結果
2.2、post請求亂碼
post請求可以在獲得指定引數之前設定編碼,如果沒有指定編碼,預設為iso-8859-1【tomcat6預設是iso-8859-1,tomcat8預設是utf-8】
最常用的是
req.setCharacterEncoding(“utf-8”);
String username=req.getParameter("username");
也可用跟get一樣的方式
String username=req.getParameter("username");
username=new String(username.getBytes("iso-8859-1"),"utf-8");
3、響應編碼
響應時伺服器端預設的也是iso-8859-1,可以通過設定響應頭的方式
response.setContentType(“text/html;charset=utf-8”);
4、Filter和裝飾者模式解決所有的中文亂碼問題
##過濾器
package cn.hfits.servletS.demo3;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author yj
* @時間:2018-10-15 上午09:15:57
* @description:解決全站中文亂碼 tomcat 6下
*/
public class CharacterEncodingFilter implements Filter{
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp;
request.setCharacterEncoding("utf-8");//解決post亂碼
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");//解決響應亂碼
System.out.println("經過過濾器:CharacterEncodingFilter");
chain.doFilter(new MyRequest(request), response);
}
/*
* HttpServletRequestWrapper 裝飾者模式
*/
class MyRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request=request;
}
@Override
public String getParameter(String name) {
String value=this.request.getParameter(name);
if(value==null)
{
return null;
}
if(!this.request.getMethod().equalsIgnoreCase("get"))
{
return value;
}
try {
//解決get亂碼 在tomcat6下 如果是tomcat8return new String(value.getBytes("utf-8"), request.getCharacterEncoding());
return new String(value.getBytes("ISO8859-1"), request.getCharacterEncoding());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
##xml配置
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>cn.hfits.servletS.demo3.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>