Hi all,

I had posted this problem on the Struts Mailing list but never got any
response. So I thought I might forward it on to this list. Please let me
know if the problem sounds familiar.

Thanks,
Jitesh

-----Original Message-----
From: Jitesh Vidhani [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 11:49 AM
To: [EMAIL PROTECTED]
Subject: Problem writing a Filter with Struts


Hi all,

I am trying to write a Filter around my struts layer.

Basically what I want to achieve is that when the User tries to go to a
particular URL, I make him go through the Filter. The filter calls
chain.doFilter() and I then I check to see if the ActionBean has set up a
particular attribute in the request. If it has been set then the Filter
modifies the response and outputs a different HTML onto the client.

Heres the code snippet from my Filter:

                ServletOutputStream out = response.getOutputStream();
                ResponseWrapper wrapper = new
ResponseWrapper((HttpServletResponse)response);
                chain.doFilter(request, wrapper);

                if(request.getAttribute("Modify")!=null)
                {
                        CharArrayWriter caw = new CharArrayWriter();
                        caw.write("<html><body>THIS IS THE MODIFIED 
PAGE</body></html>");
                        response.setContentLength(caw.toString().length());
                        out.print(caw.toString());
                }
                else
                {
                        byte[] bytes = wrapper.toByteArray();
                        System.out.println("About to add: " + 
String.valueOf(bytes.length) + "
bytes to the stream: " + new String(bytes));
                        out.write(bytes);
                        response.setContentLength(bytes.length);
                }
                response.setContentType("text/html");
                out.flush();
                out.close();

I have a wrapper wherein I extend the HttpServletResponseWrapper and use a
ByteArrayOutoutStream and I have overridden the getWriter() and the
getOutputStream() methods of the HttpServletResponseWrapper.

Now when I try to use this URL in the client browser then I somehow see a
Blank page irrespective of whether the request attribute has been set or
not. I also tried to print the Bytes from the wrapper that I am writing out
to the response's ServletOutputStream and it seems to be right.

I have written a small test servlet and the Filter works just fine with that
Servlet. So there is something perculiar about the Struts ActionServlet that
doesnt allow me to do this.

I am not able to figure out where I am going wrong. Any help would be
appreciated.

Thanks,
Jitesh

_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.basebeans.com:8081/mailman/listinfo/mvc-programmers

Reply via email to