Dears,
I'm having troubles in doing a page hit counter Filter wrapping
HttpServletResponse(s) with the purpose of obtaining the count of emitted
bytes.
In my specialization of the HttpServletResponseWrapper I basicly catch
getOutputStream() requests wrapping the ServletOutputStream returned by the
Tomcat 5.x implementation to a specialized version of ServletOutputStream
(see below). getWriter() request are catched as well returning a PrintWriter
instance over my specialized ServletOutputStream (see below, too).
The problem is that, while this works for servlets invoking getOutputStream(),
it doesn't for servlets (or, at least, jsp) which invoke getWriter(): no
CounterOutputStream's byte write() methods are invoked during the jsp
processing, the output stream received by the browser is empty with a 200
status code.
I can't find why my code is acting this way. Do you have any clue to spare?
Finally, a cultural question. I see that the ServletOutputStream specification
is somehow a mix between an OutputStream (infact, it extends it) and,
basicly, a PrintWriter (it has a lot of print(), println() and string-base
write() methods which, I guess, shouldn't be there). Does anybody know why
ServletOutputStream is so messy? Hystorical reasons?
Thank you in advance for you attentions,
Giampaolo Tomassoni
-- code --
protected static class CounterResponseWrapper
extends HttpServletResponseWrapper {
protected interface Countable {
public int getCount();
};
protected static class CounterOutputStream
extends ServletOutputStream
implements Countable {
public ServletOutputStream sos;
public void write(byte[] b, int off, int len)
throws IOException {
sos.write(b, off, len);
nWrittenBytes += len;
}
public void write(byte[] b)
throws IOException {
sos.write(b);
nWrittenBytes += b.length;
}
public void write(int b)
throws IOException {
sos.write(b);
nWrittenBytes++;
}
public void flush()
throws IOException
{ sos.flush(); }
public void close()
throws IOException
{ sos.close(); }
public int nWrittenBytes;
public int getCount()
{ return(nWrittenBytes); }
public CounterOutputStream(ServletOutputStream sos) {
super();
this.sos = sos;
nWrittenBytes = 0;
}
}
...
public Countablecountable = null;
public int getCount()
{ return(countable == null ? 0 : countable.getCount()); }
public ServletOutputStream getOutputStream()
throws IOException {
if(countable != null)
// This is to accomplish servlet specifications
throw new IllegalStateException();
CounterOutputStream cos = new
CounterOutputStream(super.getOutputStream());
countable = cos;
return(cos);
}
public PrintWriter getWriter()
throws IOException {
return(
new PrintWriter(
new OutputStreamWriter(
getOutputStream(),
getCharacterEncoding()
),
true
)
);
}
...
}
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]