在使用 request 的 getReader方法獲取請求體資料的時候
代碼:
@WebServlet("/std4")
public class ServletDemo04 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedReader reader = request.getReader();
String len=null;
while ((len=reader.readLine())!=null){
//直接寫到頁面
response.getWriter().write(len);
//在控制器里列印
System.out.println(len);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
我的表單:
<form action="${pageContext.request.contextPath}/std4" method="post">
用戶名:<input type="text" placeholder="請輸入用戶名" name="username"/> <br>
密 碼: <input type="password" placeholder="請輸入密碼" name="pwd"/> <br>
<input type="submit" value="提交">
</form>
然后呢 啟動后報了 一堆亂碼

我是直接用response.getWriter().write() 在頁面列印出來的 一開始只是認為沒有 設定編碼的問題
response.setContentType("text/html;charset=UTF-8"); 設定瀏覽器端解碼
request.setCharacterEncoding("UTF-8"); //設定從request中取得的值或從資料庫中取出的值,
加上以后還是不行 ,但是在瀏覽器的除錯器里是這樣的

是不是 jsp頁面編碼的問題啊?
然后呢 我在jsp頁面 form表單上加了一個 enctype="text/plain 神奇的問題就解決了

enctype: 屬性規定在發送到服務器之前應該如何對表單資料進行編碼,
text/plain : 空格轉換為 “+” 加號,但不對特殊字符編碼,(懵逼····)
大概意思 就是發送純文本內容
希望有 大佬來指點一下
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/61422.html
標籤:其他
下一篇:云計算前景
