HttpServletResponse
// 設(shè)置瀏覽器編碼
public class ServletDemo11 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 服務(wù)器中默認(rèn)的編碼為ISO-8859-1,不支持中文(tomcat默認(rèn))
response.setCharacterEncoding("utf-8");// 使用utf-8編碼響應(yīng)客戶端
// 響應(yīng)消息頭中加入說明,高速瀏覽器使用哪種編碼
response.setContentType("text/html;charset=utf-8");// 方式1
response.setHeader("content-type", "text/html;charset=utf-8");// 方式2
PrintWriter out = response.getWriter();// 得到一個字符輸出流
out.write("你好");// 向客戶端響應(yīng)內(nèi)容
ServletOutputStream sos = response.getOutputStream();
sos.write("你好123".getBytes());// 使用getBytes可以使用默認(rèn)客戶端編碼
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。