HttpServletResponse lose header if write body before addHeader?

匆匆过客 提交于 2019-12-11 02:59:22

问题


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

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);

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

then it's ok both on linux and windows.

This is why?


回答1:


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.

Reasons for response getting committed



来源:https://stackoverflow.com/questions/23190944/httpservletresponse-lose-header-if-write-body-before-addheader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!