Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 511851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:15:34+00:00 2026-05-13T07:15:34+00:00

Is there any way with which I can capture generated dynamic content on the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:15:34+00:00Added an answer on May 13, 2026 at 7:15 am

    If the request is idempotent (such as GET requests are), then just use java.net.URL to get an InputStream of the JSP output. E.g.

    InputStream input = new URL("http://localhost/context/page.jsp").openStream();
    

    If the request is not idempotent (such as POST requests are), then you need to create a Filter which wraps the ServletResponse with a custom implementation of the PrintWriter with the five write() 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.

    package mypackage;
    
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.Writer;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;
    
    public class CopyResponseFilter implements Filter {
    
        public void init(FilterConfig config) throws ServletException {
            // NOOP.
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
            throws IOException, ServletException
        {
            // Set character encoding for better world domination.
            response.setCharacterEncoding("UTF-8");
    
            // Create copy writer.
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            CopyWriter copyWriter = new CopyWriter(new OutputStreamWriter(
                httpResponse.getOutputStream(), httpResponse.getCharacterEncoding()));
    
            // Filter request with response which is wrapped with new writer.
            chain.doFilter(request, wrapResponse(httpResponse, copyWriter));
    
            // Store the copy writer afterwards in session so that it's available in next request.
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            httpRequest.getSession().setAttribute("copyWriter", copyWriter);
        }
    
        public void destroy() {
            // NOOP.
        }
    
        private static HttpServletResponse wrapResponse
            (final HttpServletResponse response, final PrintWriter writer)
        {
            return new HttpServletResponseWrapper(response) {
                public PrintWriter getWriter() throws IOException {
                    return writer;
                }
            };
        }
    
    }
    
    class CopyWriter extends PrintWriter {
    
        StringBuilder copy = new StringBuilder();
    
        public CopyWriter(Writer out) {
            super(out);
        }
    
        public void write(int c) {
            copy.append((char) c); // It is actually a char, not an int.
            super.write(c);
            super.flush();
        }
    
        public void write(char[] chars) {
            copy.append(chars);
            super.write(chars);
            super.flush();
        }
    
        public void write(char[] chars, int offset, int length) {
            copy.append(chars, offset, length);
            super.write(chars, offset, length);
            super.flush();
        }
    
        public void write(String string) {
            copy.append(string);
            super.write(string);
            super.flush();
        }
    
        public void write(String string, int offset, int length) {
            copy.append(string, offset, length);
            super.write(string, offset, length);
            super.flush();
        }
    
        public String getCopy() {
            return copy.toString();
        }
    
    }
    

    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 CopyWriter in the session:

    CopyWriter copyWriter = (CopyWriter) request.getSession().getAttribute("copyWriter");
    String outputOfPreviousRequest = copyWriter.getCopy();
    

    Note that you should map this filter on an url-pattern covering 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-pattern or another way of storing it in session, e.g. in flavor of a Map<URL, CopyWriter> or so.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.