• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Making Request object available to stream

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to capture a snapshot of the HTML of a page at a certain stage. If I call the struts action class from a browser, this page comes up perfectly with the correct values where the JSP write tags resolved. When I try calling this struts action class from inside a different action class and capture the HTML in a buffer, the values that should be in the request object are not so the write tags produce nothing. I have tried calling this code snippet from a different JSP where the correct formbeans were in the request object and it still didn't produce the values. Here is my code snippet:

StringBuffer fullReportPath = new StringBuffer();
fullReportPath.append("http://devServer1:9080/ofroiWeb/FinalForm.do");

URL url = new URL(fullReportPath.toString());
BufferedReader br = null;

br = new BufferedReader(new InputStreamReader(url.openStream()));

StringBuffer buffer = new StringBuffer();

String str;
while ((str = br.readLine()) != null) {
buffer.append(str);
}

// Close the input stream and return bytes
br.close();


I have been thinking about a workaround, where I put the formbean in the session and then change the JSP to look in the session for it rather than the request object, but I don't want to do this unless I have to.

Thanks,
Alan
 
Alan Peltz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in case anyone else was wondering, I found a solution to this on another forum.

RequestDispatcher rd = request.getRequestDispatcher("ofroiWeb/FinalForm.do");
// wrap the original response wrapper in a buffered response
BufferedHttpResponseWrapper buffResponse = new BufferedHttpResponseWrapper(response);

try {
// use the request dispatcher to invoke our page
// the response will be written into the buffResponse object.
rd.include(request, buffResponse);
} catch (ServletException e) {
throw e;
}

// retrieve the result
byte[] byteResult = buffResponse.getBuffer();
String result = new String(byteResult);

StringBuffer buffer = new StringBuffer();
buffer.append(result);

This solution worked like a charm. The guy that posted it said it only works if this call happens on the same server as the original call. BufferedHttpResponseWrapper is a class I had to create. I found good samples for this class at http://www.porcupyne.org/docs/browse_source/JavaXslt/com/oreilly/javaxslt/util/BufferedHttpResponseWrapper.java.html.

As you see I don't use a URL object at all. I use a RequestDispatcher instead. I use the BufferedHttpResponseWrapper so I could update the response object. I really don't have my mind completely wrapped around this solution, but it works so I'm happy.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic