gezhonglunta
gezhonglunta

Reputation: 1391

HttpServletResponse lose header if write body before addHeader?

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?

Upvotes: 2

Views: 1263

Answers (1)

Ramesh PVK
Ramesh PVK

Reputation: 15446

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

Upvotes: 3

Related Questions