permaculture playing cards*
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
Author

Servlet response get output data

Sandip Chaudhuri
Greenhorn

Joined: Dec 27, 2004
Posts: 26
Hi,
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
Greenhorn

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(
(HttpServletResponse)response);
chain.doFilter(request, responseWrapper);
StringBuffer str = new StringBuffer(responseWrapper.toString());
out.write(str.toString());

where CharResponseWrapper is

public class CharResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;

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

public CharResponseWrapper(HttpServletResponse response) {
super(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.
Sandip
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
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.
 
jQuery in Action, 2nd edition
 
subject: Servlet response get output data