【Java】【web】HttpServletResponse

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)