Html表單提交到Servlet輸出到頁面亂碼
阿新 • • 發佈:2019-01-22
con request Language pre gbk 瀏覽器 get 獲取 12px
Html使用的編碼是UTF-8編碼顯示頁面,之後使用form表單提交字段到Servlet中,Servlet將利用getParamer方法獲得form提交的字段,之後通過Respone中的writer將獲取到的前臺字段反饋到前臺中去,出現中午亂碼錯誤,這是因為添加了這個字段。 response.setContentType("text/html"); 可見瀏覽器對後臺提交的字段的一個識別作用,這個字段代表提交的字段是“文本/html”,給瀏覽器識別,這樣後臺提交文本時候的時候會出現中文字符是以文本的形式提交的,而提交的字段是使用GBK格式(因為能夠在終端正確顯示)必然因為輸入和輸出流的不同,導致中文字符出現編碼問題,所以必須對輸入和輸出流進行編碼通化,具體方法就是加如下代碼: // 設置輸入的編碼格式為utf-8 request.setCharacterEncoding("utf8"); // 設置輸入的編碼格式為utf-8 response.setCharacterEncoding("utf8"); 完整的操作片段為: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置輸入的編碼格式為utf-8 request.setCharacterEncoding("utf8"); // 設置輸入的編碼格式為utf-8 response.setCharacterEncoding("utf8"); // 給瀏覽器的標示,該字體為text/html response.setContentType("text/html"); PrintWriter out = response.getWriter(); // 做一些輸出操作........... // out.println(); out.flush(); out.close(); }
Html使用的編碼是UTF-8編碼顯示頁面,之後使用form表單提交字段到Servlet中,Servlet將利用getParamer方法獲得form提交的字段,之後通過Respone中的writer將獲取到的前臺字段反饋到前臺中去,出現中午亂碼錯誤,這是因為添加了這個字段。
response.setContentType("text/html");
// 設置輸入的編碼格式為utf-8
request.setCharacterEncoding("utf8");
// 設置輸入的編碼格式為utf-8
response.setCharacterEncoding("utf8");
publicvoid doPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException{
// 設置輸入的編碼格式為utf-8
request.setCharacterEncoding("utf8"
// 設置輸入的編碼格式為utf-8
response.setCharacterEncoding("utf8");
// 給瀏覽器的標示,該字體為text/html
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 做一些輸出操作...........
// out.println();
out.flush();
out.close();
}
Html表單提交到Servlet輸出到頁面亂碼