aspose file tools*
The moose likes Servlets and the fly likes Getting contents form the Response Object 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 "Getting contents form the Response Object" Watch "Getting contents form the Response Object" New topic
Author

Getting contents form the Response Object

Mani Balasubramanian
Greenhorn

Joined: Jan 03, 2002
Posts: 15
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

Joined: Jan 03, 2002
Posts: 15
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting contents form the Response Object