Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to pass count from servlet to javascript

Avatar

Level 3

Hello ,

      How to pass  javax.jcr.NodeIterator iter =toNode.getNodes(); 
                          long count =iter.getSize();

count value  from servlet to javascript.,that would be great for your reply.

 

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi, 
this is a bit more tricky since the JavaScript and the JSP code doesn't really execute in the same environment. There is not a direct communication between them both but take a look at this (really simple) example of how you could pass a Java variable to the JavaScript environment:

 

// MY JSP FILE <% //.. previous stuff javax.jcr.NodeIterator iter =toNode.getNodes(); long count =iter.getSize(); //..... more stuff %> <script type="text/javascript"> var myCountValue = "<%=count%>"; </script>




Another way of actually doing this would be to make an ajax call from a JavaScript function to the servlet. Then make sure that the servlet will return a JSON answer that the JavaScript function can parse and then use. 

Hope that will point you in some direction. 

/Johan

View solution in original post

2 Replies

Avatar

Correct answer by
Level 7

Hi, 
this is a bit more tricky since the JavaScript and the JSP code doesn't really execute in the same environment. There is not a direct communication between them both but take a look at this (really simple) example of how you could pass a Java variable to the JavaScript environment:

 

// MY JSP FILE <% //.. previous stuff javax.jcr.NodeIterator iter =toNode.getNodes(); long count =iter.getSize(); //..... more stuff %> <script type="text/javascript"> var myCountValue = "<%=count%>"; </script>




Another way of actually doing this would be to make an ajax call from a JavaScript function to the servlet. Then make sure that the servlet will return a JSON answer that the JavaScript function can parse and then use. 

Hope that will point you in some direction. 

/Johan

Avatar

Level 10

When you need to have a JSP get back values from Java, setup your Java as a CQ bundle and call a method of the OSGi bundle to get back the return value.  

For example - assume this is your Java class deployed within an OSGi:

//This is a component so it can provide or consume services
@Component
  
@Service
public class CountServiceImpl implements CountService {
  

//method that returns count
public int getCount()

{

       ... 
       javax.jcr.NodeIterator iter =toNode.getNodes(); 
        long count =iter.getSize();

        retrurn count

}

From your JSP -- you can use sling to get an instance of CountService and invoke getCount:

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.commons.json.io.*,com.adobe.cq.*" %><%
S
  
com.adobe.cq.CountService cs = sling.getService(com.adobe.cq.CountService .class);
  
int myPK = cs.getCount() ;
   
%>

 

To see an article that puts this all together and uses the JCR API from within an OSGi bundle -- see:

http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

 

Hope this helps