aspose file tools*
The moose likes Servlets and the fly likes How to get content from HttpServletResponse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get content from HttpServletResponse" Watch "How to get content from HttpServletResponse" New topic
Author

How to get content from HttpServletResponse

Sumved Shami
Greenhorn

Joined: Aug 18, 2009
Posts: 3
Hi All

I have written a filter on top of a Servlet. After doFilter() method, I want to get the content from the response object and save it inside cache. The content in the response is set by PrintWriter.println(xmlContent).

Here is the code snippet:



Please look at this line in the code snippet:
String xmlContent = response.getContent();

Right now, getContent() method is not available in HttpServletResponseWrapper class. But, I want a work around for this.

Thanks for showing your interest in this post.

Regards,
Sumved Shami
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
A filter is only the entry point for the request. The filter is not invoked once the servlet is done and returns it's results.

That means youh have to work on the HttpServletResponseWrapper and capture whatever is written to the response.


JDBCSupport - An easy to use, light-weight JDBC framework -
Sumved Shami
Greenhorn

Joined: Aug 18, 2009
Posts: 3
Thanks for such a quick reply Sebastian.

So, you mean, I need to extend HttpServletResponseWrapper class?
Even if I extend a class, where I can get this response content? Precisely, where and how I will capture the content?

Thanks for your patience.



Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The ResponseWrapper is the response object.

You wrapper the original response with the wrapper class, which implements the ServletResponse interface.
Every method called on the ServletResponse is propagated to your response wrapper.

So you just extend the Wrapper class and override the methods you need.
Sumved Shami
Greenhorn

Joined: Aug 18, 2009
Posts: 3
Thanks for your reply again.

But, precisely which method needs to be over-written?

Thanks & Regards,
Sumved Shami
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
if you want to capture the content that is written using response.println() then override println().

The idea is simple. The wrapper enables you to provide your own implementation of any ServletResponse method.

So if you want to capture the output written to the response, override the getWriter() method and return your own implementation of a Writer (which in your case would be a wrappered writer).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get content from HttpServletResponse