如果在 addHeader 之前写入正文,HttpServletResponse 会丢失标头吗? [英] HttpServletResponse lose header if write body before addHeader?

查看:35
本文介绍了如果在 addHeader 之前写入正文,HttpServletResponse 会丢失标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Java HotSpot(TM) 64 位服务器 VM(构建 16.3-b01,混合模式),tomcat6

Environment: Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode),tomcat6

当我使用HttpServlet发送一个html页面时,像这样:

When I use HttpServlet to send a html page,like this:

resp.getWriter().append(body);//"body" is a html file content,and has some \n at first
resp.addHeader(name, value);

代码在Linux上运行时,http客户端无法获取我添加的header,但在Windows7上可以.

When the codes run on Linux,the http client can not get the header that I added,but run on Windows7 it can.

我交换订单代码,如下所示:

I exchange order the codes,like this:

resp.addHeader(name, value);
resp.getWriter().append(body);//"body" is a html file content

那么在 linux 和 windows 上都可以.

then it's ok both on linux and windows.

这是为什么?

推荐答案

提交响应后无法写入标头.如果将响应的某些部分写入客户端,则称响应已提交.由于多种原因,响应被提交的原因有多种.

Headers cannot be written once the response is committed. Response is said to be committed if some part of the response is written to the client. There are several reasons why the response gets committed due to several reasons.

响应被提交的原因

这篇关于如果在 addHeader 之前写入正文,HttpServletResponse 会丢失标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆