Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture generated dynamic content at server side

Tags:

java

jsp

servlets

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.

like image 887
Vishwanath Avatar asked Dec 26 '09 11:12

Vishwanath


People also ask

What is Dynamic Server content?

Dynamic content (aka adaptive content) refers to web content that changes based on the behavior, preferences, and interests of the user. It refers to websites as well as e-mail content and is generated at the moment a user requests a page.

How is dynamic content generated by the tiny web server?

Dynamic content is mostly powered by applications and scripts that run on the server hosting the website. When a user makes a request, these applications work in tandem with the web server to parse the request, generate content based on the request, and deliver the content to the user as though it were static content.

What is dynamic content on a website?

Dynamic content is defined as any digital or online content that changes based on data, user behaviour and preferences. This can be text, audio or video format content.

How the dynamic content is generated in JSP?

Just like JSP pages, JSP documents can generate dynamic content using expressions language (EL) expressions, scripting elements, standard actions, and custom tags. The books. jspx document uses EL expressions and custom tags to generate the XML book data.


1 Answers

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.

like image 65
BalusC Avatar answered Oct 19 '22 08:10

BalusC