如何在servlet中获取response对象

2024-11-26 00:27:23
推荐回答(1个)
回答1:

一、response对象
1.1向客户端发送数据,以字节为单位(一定要先设置编码方式,再输出)

[java] view plaincopy
String data = "你好,中国1";
OutputStream out = response.getOutputStream();
out.write(data.getBytes());//查找本机默认编码进行编码

[java] view plaincopy
String data = "你好,中国2";
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));//以UTF-8进行编码
//告诉浏览器编码方式
response.setHeader("Content-Type","text/html;charset=UTF-8"