pageEncoding設置JSP源文件和響應正文中的字符集編碼.
contentType設置JSP源文件的字符集編碼和響應正文的MIME類型.
可見,pageEncoding和contentType都可以設置JSP源文件和響應正文中的字符集編碼.但也有區別:
設置JSP源文件字符集時,優先級爲 pageEncoding > contentType.都沒有設置,默認ISO-8859-1.
設置響應輸出的字符集時,優先級爲 contentType > pageEncoding.都沒有設置,默認ISO-8859-1;都設置了,以contentType爲準.實際上,response.setContentType()就是根據contentType屬性設置的
在servlet和action的時候
設置響應屬性一定要記得加班方法開始的最前端
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setAttribute("json", obj2.toString());
request.getRequestDispatcher("json.jsp").forward(request, response);
或者直接輸出到頁面中也可以
之前也要設置編碼類型
//PrintWriter out = response.getWriter();
// out.write(obj2.toString());