Is there any way with which I can capture generated dynamic content on the server side and get that file or string object of the same to the servlet.
We can generate dynamic content with JSPs, but we dont have access to the generated dynamic content at the server side. As soon as we do the forward container generates the dynamic content and sends it to response.
I need access to generated dynamic content on the server side.
Any help will be appreciated.
If the request is idempotent (such as
GETrequests are), then just usejava.net.URLto get an InputStream of the JSP output. E.g.If the request is not idempotent (such as
POSTrequests are), then you need to create aFilterwhich wraps theServletResponsewith a custom implementation of thePrintWriterwith the fivewrite()methods been overridden wherein you copy output into some buffer/builder which you store in the session or a temporary folder at local disk file system so that it can be accessed afterwards in the subsequent requests. E.g.You can access the final output in any servlet of the subsequent request (note that you cannot access it in any servlet of the current request, because it’s already too late to do something with it) by just accessing the
CopyWriterin the session:Note that you should map this filter on an
url-patterncovering the JSP pages of interest and thus not on/*or so, otherwise it would run on static files (css, js, images, etc) which are included in the same JSP as well.Also note that multiple requests inside the same session would override each other, it’s up to you to distinguish between those requests by using a proper
url-patternor another way of storing it in session, e.g. in flavor of aMap<URL, CopyWriter>or so.Hope this helps.