Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get content from HttpServletResponse

 
Sumved Shami
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sumved Shami
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply again.

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

Thanks & Regards,
Sumved Shami
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic