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

Servlet response get output data

Sandip Chaudhuri

Joined: Dec 27, 2004
Posts: 26
I am required to create a logger which has the values of the data being send for processing as well as the data being send back so that we can diferentiate to find the changes that have been made.
We are using the Strus framework so all the processing goes through a single class.
I have used the request.getParameterMap() and request.getParameter(String paramatername).
However once i pass it to the filter and get the response i am at a loss of how to get the values going in the response.
Do i have to somehow convert the outputstream odf the response into an inputstream and then parse the HTML or something like that? I am totally at a loss. Please advice.
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Using filters you can modify both the request and also the response. Please dig into the filter concept properly
Sandip Chaudhuri

Joined: Dec 27, 2004
Posts: 26
HI rajesh i looked into the Filter.
but i am not sure exactly what you were referring to.
This is what i have done

PrintWriter out = response.getWriter();
CharResponseWrapper responseWrapper = new CharResponseWrapper(
chain.doFilter(request, responseWrapper);
StringBuffer str = new StringBuffer(responseWrapper.toString());

where CharResponseWrapper is

public class CharResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;

public String toString() {
return output.toString();

public CharResponseWrapper(HttpServletResponse response) {
output = new CharArrayWriter();

public PrintWriter getWriter() {
return new PrintWriter(output);

Now i have the complete HTML page as a stringBuffer.
But to get the value of the form elements i have to parse etc which i dont think is a great idea since the accuracy goes down quiet a lot.
Am i missing something in the filter or is there any other way?
Thanks for the help.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2068
One way is to use the filter on the servlet that generates the data. This is the servlet that will pass the data that the jsp needs to complete the html.
I agree. Here's the link:
subject: Servlet response get output data
It's not a secret anymore!