Hi EveryOne ,
I have a servlet that uses some request parameters and based on that sets a JSON Response.
Is it possible to use this in my JSP (I do not have a form) .
I want to invoke the servlet by passing the parameters and obtain the JSON Response and display this content on my JSP. I dont want to copy the same code to my JSP because this servlet is already being used by several classes and hence would like re-use the same.
Thanks in Advance
Harish
Solved! Go to Solution.
You can invoke AEM servlets from an AEM JSP. We have community articles on this subject. See this one:
http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html
This servlet also encodes posted data as JSON as well.
Here is a more advanced article that talks about invoking a Servlet from a CQ widget that you drag from the CQ sidekick:
http://helpx.adobe.com/experience-manager/using/creating-custom-cq-tree.html
Views
Replies
Total Likes
You can use SlingRequestProcessor.
http://sling.apache.org/apidocs/sling6/org/apache/sling/engine/SlingRequestProcessor.html
You can try something like the following;
private RequestResponseFactory requestResponseFactory;
private SlingRequestProcessor requestProcessor;
private ResourceResolver resourceResolver;
private JSONObject getResourceJSON(Resource resource, Map<String, Object> anyOtherRequestParams) {
JSONObject jsonObject = null;
String requestURL = null;
try {
requestURL = resource.getPath() + ".json";
HttpServletRequest request = this.requestResponseFactory.createRequest("GET", requestURL, anyOtherRequestParams);
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse response = requestResponseFactory.createResponse(out);
this.requestProcessor.processRequest(request, response, this.resourceResolver);
jsonObject = new JSONObject(new String(out.toByteArray(), STRING_ENCODING));
} catch (Exception ex) {
LOGGER.warn("Failed to export resource as JSON " + resource + "," + requestURL, ex);
}
return jsonObject;
}
You can invoke AEM servlets from an AEM JSP. We have community articles on this subject. See this one:
http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html
This servlet also encodes posted data as JSON as well.
Here is a more advanced article that talks about invoking a Servlet from a CQ widget that you drag from the CQ sidekick:
http://helpx.adobe.com/experience-manager/using/creating-custom-cq-tree.html
Views
Replies
Total Likes
We can achieve it using an ajax call to the servlet as well .I would prefer something like this
function myFunction(){
var urlBulder='/myservletpath?param1=xxx¶m2=yyy';
$.ajax({
type: 'POST',
url: urlBulder,
dataType: 'json',
error : function() {
alert('Error in getting the response from server');
},
success: function(data) {
// logic here
}
});
}
You can call this javascript ajax function and perform the logic on the received json to display/modify any elements on the jsp .
Regards,
Krishna
Views
Likes
Replies
Views
Likes
Replies