How to avoid sending Java requests from the Java Web server to itself?
The reality is that the Java Web server (Weblogic) receives requests from users and must send a zip archive in response The archive must be generated dynamically from some files required by the user and an HTML report generated by the server itself I want to reuse the JSF servlet that the server has used in other cases to generate this report So basically, I use:
HttpURLConnection self = new URL ("http://me.myself.com/report.jsf?...").openConnection (); String report_html = fetchHtmlFromConnection (self);
Then create the requested zip, including the generated HTML
The question is, can I avoid issuing an internal HTTP request (report. JSF) in this case? This involves basically meaningless (because applications just "talk" about themselves), through the operating system, httpd (possibly on different machines), and so on
Solution
I'm not very familiar with JSF, but from what I know, you can use technologies that also apply to JSP pages:
>Create your own httpservletresponsewrapper (the class used by the container, which allows you to modify the response) > use it to override the default writer (write the rendered page to the output), and provide a way to write the output to a string or a temporary file, which will provide compressed code
There is a beautiful and simple tutorial that shows you how to do this: http://blog.valotas.com/2011/09/get-output-of-jsp-or-servlet-response.html
then
>As shown by Gyan, get a servletrequestdispatcher from your servlet, let you call JSF rendering > forward servlet call to provide your own httpservletresponsewrapper > use your httpservletresponsewrapper to obtain the rendered HTML and provide it to the compressed code
So compressing servlets is like:
TempFileRespWrapper respWrapper = new TempFileRespWrapper(); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/report.jsf"); dispatcher.forward(request,respWrapper); File f = respWrapper.getOutputPath(); addFileToZip(f);