Servlet的亂碼處理手記
前記
今天開學第二天,老師教了我們如何處理Servlet中的亂碼。涵蓋了所有亂碼的處理。
1、處理post方法傳入的亂碼
-----修改servlet中的亂碼
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
----設定頁面展示的編碼
resp.setHeader("Content-type", "text/html;charset=UTF-8");
resp.setContentType("text/html;charset=utf-8");
2、處理get方法傳入資料亂碼問題
-----修改servlet中的亂碼
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
----設定編碼的轉換
String name = new String(request.getParameter("userName").getBytes("iso8859-1"),"utf-8");
----設定頁面展示的編碼
resp.setHeader("Content-type", "text/html;charset=UTF-8");
resp.setContentType("text/html;charset=utf-8");
修改get方法傳入的亂碼:
可以通過修改tomcat中編碼設定: tomcat/conf/server.xml檔案
原文是
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
修改後:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"
URIEncoding="utf-8"/>