【Java】【web】HttpServletResponse
作者:
JerichoPH | 来源:发表于
2017-04-08 09:59 被阅读11次
HttpServletResponse
// 设置浏览器编码
public class ServletDemo11 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 服务器中默认的编码为ISO-8859-1,不支持中文(tomcat默认)
response.setCharacterEncoding("utf-8");// 使用utf-8编码响应客户端
// 响应消息头中加入说明,高速浏览器使用哪种编码
response.setContentType("text/html;charset=utf-8");// 方式1
response.setHeader("content-type", "text/html;charset=utf-8");// 方式2
PrintWriter out = response.getWriter();// 得到一个字符输出流
out.write("你好");// 向客户端响应内容
ServletOutputStream sos = response.getOutputStream();
sos.write("你好123".getBytes());// 使用getBytes可以使用默认客户端编码
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
本文标题:【Java】【web】HttpServletResponse
本文链接:https://www.haomeiwen.com/subject/cthlattx.html
网友评论