在AEM的Servlet中获取JSP输出 [英] Get JSP output within Servlet in AEM

查看:135
本文介绍了在AEM的Servlet中获取JSP输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CQ中,我们需要在servlet中处理jsp,然后将结果与我们从服务器获得的其他结果合并,然后再写回浏览器。

In CQ, we need to process jsp within servlet then combine the result with other results we get from server before writing back to browser.

以下代码几乎是我们需要什么,除了它在处理jsp后将结果写回浏览器。

The following code is almost what we need, except that it writes the result back to browser after processing jsp.

RequestDispatcher dispatcher = request.getRequestDispatcher(resource);
dispatcher.forward(request, response);

我们尝试使用模拟回复,如下所示:

We tried using mock response as follows:

RequestData requestData = new RequestData(slingRequestProcessor, request, mockResponse);
SlingHttpServletRequest slingRequest = requestData.getSlingRequest();
SlingHttpServletResponse slingResponse = requestData.getSlingResponse();
RequestDispatcher dispatcher = request.getRequestDispatcher(resource);
dispatcher.forward(slingRequest, slingResponse);

但我们遇到问题。

推荐答案

首先 - 根据您的描述,您似乎想要使用 requestDispatcher.include 而不是 requestDispatcher.forward

First off - based on your description it sounds like you want to use requestDispatcher.include and not requestDispatcher.forward.

关于包含响应内容,您传递给 requestDispatcher.include 调用的响应可能是您自己创建的对象,它会将其输出写入字符串,而不是将其返回给浏览器。下面的内容可能是合适的:

Concerning the inclusion of the response content, the response which you pass into a requestDispatcher.include call could be an object of your own creation which would write its output to a string instead of returning it to the browser. Something along the lines of the following might be appropriate:

final ServletOutputStream outputStream = new ServletOutputStream() {
    public void write(int b) throws IOException {
        outputBuffer.append((char) b);
    }
};

SlingHttpServletResponseWrapper responseWrapper = new SlingHttpServletResponseWrapper(response) {
    public ServletOutputStream getOutputStream() {
        return outputStream;
    }

    public PrintWriter getWriter() throws IOException {
        return new PrintWriter(outputBuffer);
    }

    public SlingHttpServletResponse getSlingResponse() {
        return super.getSlingResponse();
    }
};

包含 outputStream.toString()应该提供资源请求执行的结果。

After the inclusion outputStream.toString() should provide the result of the resource request's execution.

这篇关于在AEM的Servlet中获取JSP输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆