Skip to content

Latest commit

 

History

History
109 lines (98 loc) · 5.14 KB

編碼.md

File metadata and controls

109 lines (98 loc) · 5.14 KB

編碼

常見字符編碼

iso-8859-1 (不支持中文) gbk (系統默認編碼,中國國際碼) utf-8 (萬國碼,支持全世界編碼)

響應編碼

  • 當使用 response.getWriter() 來向客戶端發送字符數據時,如果再之前沒有設置編碼,那麼默認使用 iso,因為 iso 不支持中文,會出現亂碼。
  • 在使用 response.getWriter() 之前可以使用 response.setCharacterEncoding() 來設置字符流的編碼為 gbk 或 utf-8。
  • 在使用 response.getWriter() 之前可以使用 response.setHeader("Content-type", "text/html;charset=utf-8"); 來設置響應頭,通知瀏覽器服務器使用的編碼方式。-> 這個方法不只設置響應頭,而且會設置 setChracterEncoding(),一句頂兩句。
  • setHeader("Content-Type", "text/html;charset=utf-8"); 的快捷方法: setContentType("text/html; charset=utf-8); -> 通過 Content-type 頭,得知服務器使用的編碼並運用於本地,即不會產生亂碼。
  • 想不產生亂碼: 在使用 getWriter() 方法前,先調用 response.setContentType("text/html;charset=utf-8");

請求編碼

  • 客戶端阿送給服務器的請求參數是什麼編碼: 客戶端首先要打開一個頁面,然後在頁面中提交表單或點擊超鏈接。在請求這個頁面時,服務器響應的編碼是什麼,客戶端發送請求時的編碼就是甚麼。
  • 服務器端默認使用解碼參數: ISO-8859-1,不支持中文,會出現亂碼。
  • 請求編碼處理分為兩種: GET請求參數: 不在請求體中。 POST 請求參數: 請求體中。
  • GET 請求編碼處理:
  • String username = new String(request.getParameter("iso-8859-1"), "utf-8");
  • 在 server.xml 中配置 URIEncoding=utf-8
  • POST 請求編碼處理:
  • String username = new String(request.getParameter("iso-8859-1"), "utf-8");
  • 在獲取參數之前調用 request.setCharacterEncoding("utf-8");
    <!DOCTYPE html>
    <html>
      <head>
        <title>c.html</title>
    	
        <meta name="keywords" content="keyword1,keyword2,keyword3">
        <meta name="description" content="this is my page">
        <meta name="content-type" content="text/html; charset=BIG5">
        
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    
      </head>
      
      <body>
        This is my HTML page. <br>
        <form action="/Java_Web/servlet_6/ServletDemo" method="post">
        	用戶名: <input type="text" name="username" value="李四"><br>
        	<input type="submit" name="提交">
        	<hr>
        	<a href="Java_Web/servlet_6/ServletDemo?username=張三">GET</a>
        </form>
      </body>
    </html>
    public class ServletDemo extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		/*
    		 * 1. 先獲取來使用 iso 的錯誤字符串
    		 * 2. 回退,使用 utf-8 重編
    		 */
    		String name = request.getParameter("username");
    		byte[] b = name.getBytes("iso-8859-1");
    		name = new String(b, "utf-8");
    		
    		System.out.println(name);
    	}
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		/*
    		 * 1. 在獲取參數之前,需先調用 request.setCharacterEncoding("utf-8");
    		 * 2. 使用 getParameter() 來獲取參數
    		 */
    		request.setCharacterEncoding("utf-8");
    		String username = request.getParameter("username");
    		System.out.println(username);
    	}
    }

URL 編碼

表單的類型: Content-Type: application/x-www-form-urlencoded -> 把中文轉換成 % 後面跟隨兩位的 16 進制。

  • 在客戶端和服務氣之間傳遞中文時需要把它轉換成網絡適合的方式。
  • 不是字符編碼。
  • 用來在客戶端與服務器之間傳遞參數用的一種方式。
  • URL 編碼需要先指定一種字符編碼,把字符串解碼後,得到 byte[], 然後把小於 0 的字節 +256,再轉換成 16 進制,前面再加個 %
  • POST 請求默認使用 URL 編碼,Tomcat 會自動使用 URL 解碼。
  • URL 編碼: String username = URLEncoder.encode(username, "utf-8");
  • URL 解碼: String username = URLDecoder.decode(username, "utf-8");

路徑

  • web.xml 中 路徑(Servlet 路徑)
  • 要嘛以 "*" 開頭,要嘛以 "/" 開頭
  • 轉發和包含路徑
  • 以 "/" 開頭,相對當前項目路徑,例如: http://localhost:8080/Java_Web/
  • 不已 "/" 開頭,相對當前 Servlet 路徑。
  • 重定向路徑(客戶端路徑)
  • 以 "/" 開頭: 相對當前主機,例如: http://localhost:8080/,需要自己手動添加項目名。
  • 頁面中超鏈接和表單路徑
  • 與重定向相同,都是客戶端路徑,需要添加項目名。
  • ServletContext 獲取資源路徑
  • 相對當前項目目錄,即當前 index.jsp 所在目錄。
  • ClassLoader 獲取資源路徑
  • 相對 classes 目錄
  • 不能以 "/" 開頭
  • Class 獲取資源路徑
  • 以 "/" 開頭相對 classes 目錄
  • 不以 "/" 開頭相對當前 .class 文件所在目錄。