File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Size of the response sent by the server. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Size of the response sent by the server." Watch "Size of the response sent by the server." New topic

Size of the response sent by the server.

Robin Sharma
Ranch Hand

Joined: Aug 24, 2005
Posts: 76

How do i calculate the size of the response sent by the server for a request? I am using a servlet filter which just prints the size of the request/response. Now, I can get the size of the request using the request.getContentLength() method. But, how do i get the size of the response?


There is always a bug :-)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Why do you feel you need it?

[Asking smart questions] [About Bear] [Books by Bear]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
It's quite simple. You need to wrap a response object in your object which plays a role of proxy. You should care only about two methods: getOutputStream and getWriter. Return your own implementation wrapping objects returned by original response for these methods. Your implementation is also a proxy which counts a number of processed bytes. You can use Java proxy capabilities or HttpServletResponseWrapper to simplify your implementation.

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If, on the other hand, he needs the size to set the response content-length header, this size is needed before anything is written to the response. In that case all output should be to a local buffer - see CharArrayWriter or ByteArrayOutputStream - finish the output, use the size to set the header, write the whole buffer at one go.
I agree. Here's the link:
subject: Size of the response sent by the server.
It's not a secret anymore!