Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting contents form the Response Object

 
Mani Balasubramanian
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone tell me how to get read the content of the response into a String (or any array objects)
I am sure this can be done using HttpServletResponseWrapper.
Thanks,
Mani
[ May 28, 2002: Message edited by: Mani Balasubramani ]
 
Mani Balasubramanian
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just to add that we are trying to Compress the output using filters and it works fine with servlet. But with JSP we are unable to Compress the response becasue we are not able to wrap the
response.
The problem with below code is that responseString is always returning empty for JSP but returning the content for servlet. By the way we are running it on Oracle9ias.
----------------
public class CharArrayWrapper extends HttpServletResponseWrapper
{
private CharArrayWriter charWriter;

public CharArrayWrapper(HttpServletResponse response)
{
super(response);
charWriter = new CharArrayWriter();
}

public PrintWriter getWriter()
{
return(new PrintWriter(charWriter));
}

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

public char[] toCharArray()
{
return(charWriter.toCharArray());
}
}

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws ServletException, IOException
{
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
if (!isGzipSupported(req))
{
chain.doFilter(req,res);
}
else
{
System.out.println("GZIP supported");
// Tell browser we are sending it gzipped data.
res.setHeader("Content-Encoding", "gzip");

// Invoke resource, accumulating output in the wrapper.
CharArrayWrapper responseWrapper =new CharArrayWrapper(res);
chain.doFilter(req,responseWrapper);

// Get character array representing output.
char[] responseChars = responseWrapper.toCharArray();
String responseString = responseWrapper.toString();
System.out.println("Response String..."+responseString);
// Make a writer that compresses data and puts
// it into a byte array.
ByteArrayOutputStream byteStream =new ByteArrayOutputStream();
GZIPOutputStream zipOut = new GZIPOutputStream(byteStream);
OutputStreamWriter tempOut = new OutputStreamWriter(zipOut);
// Compress original output and put it into byte array.
tempOut.write(responseChars);

// Gzip streams must be explicitly closed.
tempOut.close();

// Update the Content-Length header.
res.setContentLength(byteStream.size());
// Send compressed result to client.
OutputStream realOut = res.getOutputStream();
byteStream.writeTo(realOut);
}
}
----------
Thanks
Srinath
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic