案例學習筆記:前端:案例_過濾器Filter_解決全站亂碼問題

案例學習筆記:前端:案例_過濾器Filter_解決全站亂碼問題

 

 

案例_過濾器Filter_解決全站亂碼問題
----
核心代碼:
//解決POST的中文亂碼
request.setCharacterEncoding("utf-8");
//解決響應體的中文亂碼
response.setContentType("text/html;charset=utf-8");

搭工程:
File-->New Module, 選Java Enterprise, 選java EE6, 選Web Application,勾選Create web.xml

步驟:
1.創建jsp,設置兩個表單post/get。  (文件名:index.jsp)
2.創建過濾器 (New-->Filter),在過濾器裏解決中文亂碼問題。  (文件名:EncodingFilter.java)
3.創建UserServlet(New-->Servlet),重寫doGet/doPost方法。  (文件名:UserServlet.java)
  doGet/doPost直接獲取數據沒有中文亂碼
  直接向客戶端打印中文沒有亂碼

源代碼:
//file name: index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <h1>get方式提交中文</h1>
  <form method="get" action="/day17_anli01/userServlet">
    賬戶<input type="text" name="username" />
    <button>提交</button>
  </form>

  <h1>post方式提交中文</h1>
  <form method="post" action="/day17_anli01/userServlet">
    賬戶<input type="text" name="username" />
    <button>提交</button>
  </form>
  </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章