Servlet 解決亂碼問題
請求亂碼問題
服務器獲取的請求數據亂碼
post請求方式亂碼
設置請求編碼格式
req.setCharacterEncoding(“UTF-8”)
get請求方式亂碼
方式1
每個數據都要單獨的進行轉換
String uname1 = req.getParameter(“uname”);
String uname2 = new String(uname.getBytes(“iso-8859-1”),“utf-8”);
方式2
req.setCharacterEncoding(“UTF-8”);
在tomcat的server.xml文件中的Connector標籤中增加屬性 : useBodyEncodingForURI=“true”
響應亂碼問題
瀏覽器中顯示的服務器響應數據亂碼
設置響應編碼格式
resp.setContentType(“text/html;charset=utf-8”)
或者
resp.setHeader(“content-type”,“text/html;charset=utf-8”)