tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Giampaolo Tomassoni <>
Subject Filter, HttpServletResponseWrapper, ServletOutputStream & PrintWriter in Tomcat 5.x
Date Sat, 30 Oct 2004 11:35:33 GMT

I'm having troubles in doing a page hit counter Filter wrapping 
HttpServletResponse(s) with the purpose of obtaining the count of emitted 

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 {
		nWrittenBytes += b.length;

	    public void write(int b)
	    throws IOException {

	    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) {
		this.sos = sos;
		nWrittenBytes = 0;


	public Countable    countable = 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 
	    countable = cos;

	public PrintWriter getWriter()
	throws IOException {
		new PrintWriter(
		    new OutputStreamWriter(


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message