File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem filtering servlet response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem filtering servlet response" Watch "Problem filtering servlet response" New topic
Author

Problem filtering servlet response

KC Smith
Greenhorn

Joined: Nov 16, 2004
Posts: 7
I'm trying to filter my http response coming out of the app server - I've tested with both ATG and jboss. The problem is that when using the filter with a response wrapper the response disappears. In ATG I get a blank page, and jboss a 404.

The filter is very simple at this stage, simply getting the response and wrapping it for modification, and chaining the filter. Eventually I want to modify the html.

I can see the html output in the console, so I know the filter executes, but the page never makes it to the browser. If I change the chain.doFilter(request, buffer) to chain.doFilter(request, response) it works fine - but then I won't be able to modify the response...

I'd appreciate any thoughts, I'm running out of ideas! Thanks.

doFilter():

PrintWriter out = response.getWriter();
ResponseBuffer buffer = new ResponseBuffer((HttpServletResponse) response);
chain.doFilter(request, buffer);

// get page content
String contents = buffer.toString();
log.info(contents);

// TODO stuff here

// send the page to the browser
CharArrayWriter caw = new CharArrayWriter();
caw.write(contents);
response.setContentLength(caw.toString().length());
out.write(caw.toString());
out.close();
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I have a sample app that captures the text (writes it to System.out) and also sends it to the browser.

The code is quick and dirty but you're welcome to it.
http://simple.souther.us/capture.war

In short, it uses the wrapper pattern.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
KC Smith
Greenhorn

Joined: Nov 16, 2004
Posts: 7
Thanks for the sample. Strange, that works in jboss but not ATG. They use the same filter mappings.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I haven't touched ATG Dynamo in years.
What version of the servlet spec does it comply with?
Filters were introduced in 2.3.
KC Smith
Greenhorn

Joined: Nov 16, 2004
Posts: 7
It's ATG 6.1, uses the 2.3 spec but the old 1.3.1 JDK. That should not cause any trouble. The atg application is Struts, and the filter is set catch any .do requests and post-process the response. Your code displays the html in the console, but the result never makes it to the browser.

It seems like a chaining problem, but the application chains correctly if the original response is used in the chain.doFilter, not the wrapper. Strange...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Take a look at my filter code.
It's possible that I'm using some 1.4 functionality.

Try recompiling it with 1.3.
If it barfs, then you know where to start looking.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem filtering servlet response
 
Similar Threads
Exception coming while transforming
html processing
Caching rendered html in jsp
Servlet response get output data
when to use setContentLength()