Skip to main content
18Harry
Level 2
October 16, 2015
Solved

call a servlet from a JSP ?

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

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

3 replies

Krishna_C
Level 3
October 16, 2015

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&param2=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

Translate
smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

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

Translate
Adobe Employee
October 16, 2015

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;
    }

Translate