Azaro
Azaro

Reputation: 45

Get jsp content after forward

How can I get content of a jsp page, after servlet made a forward. At them moment I'm trying the following:

request.getRequestDispatcher(DESTINATION_PAGE).forward(request, response);
URL teamsURL = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), request.getContextPath() + DESTINATION_PAGE);
URLConnection teamsCon = teamsURL.openConnection();
String fileName = request.getServletContext().getRealPath("/") + System.currentTimeMillis() + ".html";
System.out.println(fileName);
try (BufferedReader in = new BufferedReader(new InputStreamReader(teamsCon.getInputStream()));
     PrintWriter out = new PrintWriter(fileName)) {
    String inputLine = null;
    while ((inputLine = in.readLine()) != null) {
        out.println(inputLine);
    }
}

I get the html with empty divs. But, I want the same page I see in browser.

Sorry for messy post, ask for what info you need, I'll update my post accordingly.

Upvotes: 1

Views: 388

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280011

If you're trying to get the response body after you've written it, you'll need to use a custom HttpServletResponse wrapper that keeps track of what was written to the OutputStream directly or with the Writer.

You will do this in a servlet Filter after chain.doFilter(request, yourResponseWrapper) returns. A simple example can be found here.

Upvotes: 2

Related Questions