Get JSP output in Servlet in AEM

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

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

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

We tried to use a mock answer 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);

but we get problems.

+3
source share
2 answers

At first - based on your description, it looks like what you want to use requestDispatcher.include, not requestDispatcher.forward.

, , requestDispatcher.include, , , . - :

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() .

+4

, , :

    //Map<String, Object> jstlValues, final Map<String, Object> propertyValues, String resourceType, SlingHttpServletRequest request, SlingHttpServletResponse response

    final StringWriter outputBuffer = new StringWriter();
    SlingHttpServletResponseWrapper responseWrapper = new SlingHttpServletResponseWrapper(response) {
        public PrintWriter getWriter() throws IOException {
            return new PrintWriter(outputBuffer);
        }

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

    ResourceMetadata resourceMetaData = new ResourceMetadata();
    resourceMetaData.setResolutionPath("/dummy");
    for (Map.Entry<String, Object> entry: jstlValues.entrySet()) {
        request.setAttribute(entry.getKey(), entry.getValue());
    }

    SyntheticResource resource = new SyntheticResource(request.getResourceResolver(), resourceMetaData, resourceType) {
        public <T> T adaptTo(Class<T> type) { 
             if (type == ValueMap.class) { 
                 ValueMap m = new ValueMapDecorator(propertyValues); 
                 return (T) m; 
             } 
             return super.adaptTo(type); 
         } 
    };
    RequestDispatcher dispatcher = request.getRequestDispatcher(resource);
    dispatcher.include(request, responseWrapper);

    return outputBuffer.toString();
0

Source: https://habr.com/ru/post/1598215/


All Articles