1

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.

4

1 回答 1

2

如果您在编写响应正文后尝试获取响应正文,则需要使用自定义HttpServletResponse包装器来跟踪OutputStream直接写入或使用Writer.

您将在返回Filter后在 servlet 中执行此操作。chain.doFilter(request, yourResponseWrapper)一个简单的例子可以在这里找到。

于 2013-10-01T19:51:01.997 回答