|
Response Object Part2 - Buffer
Explanation
Does this error message plague you?
Response object error 'ASP 0156 : 80004005'
Header Error
whatever.asp, line #
The HTTP headers are already written to the client browser.
Any HTTP header modifications must be made before writing page content.
The Simple Fix....
<%response.buffer=true%>
needs to be added as the very first line to any pages
made by any HTML document that mixes redirects/headers and content.
And buffering is a great idea anyway from overall server speed viewpoint see http://www.learnasp.com/advice/whybuffer.asp).
One drawback to <%response.buffer=true%> is
if a page takes a while to compose (i.e. a couple thousand records from a
database) people see nothing until the page is completely rendered. If a page
takes 20 seconds to render, the browser user sees nothing until the 20th second!
In that situation to avoid appearing as the page is dead, a well-placed
<%response.flush%> tell server to send HEADER + whatever
text so far so lets readers see the portions of the page being built. Increasing
percieved speed of database displays is explained at http://www.learnasp.com/learn/speedtables.asp
by judicous use of flushing buffer.
NT4 and Win 2000 differences
NT4 <%buffer=false%> by
default which hurts overall server speed (http://www.learnasp.com/advice/whybuffer.asp).
Win 2000 <%buffer=true%> by default. A clever administrator can change NT4
registry so buffer=true for all scripts and will see major server performance
improvements (see http://www.learnasp.com/learn/speedserver.asp
for other ones).
Here is a non-working page that will display the
error:
filename=/learn/test/daywebnotworking.asp
<%response.buffer=false%>
<html><head>
<title>dailystuff.asp</title>
</head>
<body>
<%
whatweekday=Weekday(now())
select case whatweekday
case vbSunday
response.redirect "http://www.cnn.com"
case vbMonday
response.redirect "http://www.activeserverpages.com"
case vbTuesday
response.redirect "http://www.aspalliance.com"
case vbWednesday
response.redirect "http://www.aspconvention.com"
case vbThursday
response.redirect "http://www.aspmagazine.com"
case vbFriday
response.redirect "http://www.dilbert.com"
case vbSaturday
response.redirect "http://www.movielink.com"
end select
%>
</body>
</html>
Here is the fixed page that will NOT display the
error:
filename=/learn/test/dayweb.asp
<%response.buffer=true%>
<html><head>
<title>dailystuff.asp</title>
</head>
<body>
<%
whatweekday=Weekday(now())
select case whatweekday
case vbSunday
response.redirect "http://www.cnn.com"
case vbMonday
response.redirect "http://www.amazon.com"
case vbTuesday
response.redirect "http://www.aspalliance.com"
case vbWednesday
response.redirect "http://www.theonion.com"
case vbThursday
response.redirect "http://www.dotnetweblogs.com"
case vbFriday
response.redirect "http://www.dilbert.com"
case vbSaturday
response.redirect "http://www.imdb.com"
end select
%>
</body>
</html>
Why? (The Tough Answer with Gory Details)
...first of all thanks to Ken Schaefer of Adopenstatic.com
for pointing out huge flaws and misleading information in this page as it was
written before 1/10/2001. Its revision on 1/10 was prompted by Ken. He wrote
an article on this too at http://www.adopenstatic.com/faq/headererror.asp...
That line will do away
with all "headers are already sent" messages. It essentially tells the
server don't write
anything at all to browser until
a) response.end executes thus stopping the page dead in
tracks and sending to browser
b) response.flush executes
c) 100% of the page is executed and it finishes all the
ASP and HTML.
d) response.redirect is sent (provided no content or text has been
sent with response.flush)
An ASP page must be sent to browser
in a specific strict order: first the header, and then the content. The header
could contain cookies (see http://www.learnasp.com/learn/cookies.asp),
redirects, other headers inserted by the response.addheader command and
other header information. If your page mixes content (any text) and mixes in
header info this is not following the strict rules. If the browser writes any
data that is page content, and then headers cannot be sent -- it is "too
late" -- it can't change "horses in
midstream" -- that is header info cannot come inside or after data.
Buffering does not send the page content and headers in exact order they appear
in script. Even if headers and content is mixed the header and content are sent
in correct order when buffer is flushed (either by explicit response.flush or
script completes).
|