设置响应消息
- 设置响应行
- 格式
- 设置状态码
response.setStatus(302);
- 设置响应头
response.setHeader("location","/demo1");
- 设置响应体
1
2
3
4
5
6
7
8
9
10
11
12
13/*输出字符流数据*/
PrintWriter writer = response.getWriter();
writer.write("hello response哈哈哈");
/*输出字节流数据*/
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write("hello world 哈哈哈".getBytes(StandardCharsets.UTF_8));
/*输出字节流数据-图片*/
int height = 200;
int width = 200;
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
ImageIO.write(bufferedImage,"hello",response.getOutputStream());常用功能
- 重定向
1
2
3
4
5response.setStatus(302);
response.setHeader("location","/demo1");
/*简化代码*/
response.sendRedirect("/demo1");
/*书写路径时要注意绝对路径和相对路径的区别,使用绝对路径时使用动态获取*/ - 编码设置
1
2
3
4/*设置输出流的编码*/
response.setCharacterEncoding("GBK");
/*设置Content-Type 设置的编码对服务器和客户端双向影响*/
response.setContentType("text/html;charset=utf-8");