5 Replies Latest reply on Jun 21, 2012 10:22 AM by Raja_Raju_Learner

    Accessing a Node in Servlet class




      I have a requirement to access the Node inside the Servlet class.


      How can i access it?


      In the jsp, i can access the noed like below.

      Session session1 = resourceResolver.adaptTo(Session.class);

      Node node = session1.getNode(currentPage.getPath() + "/jcr:content");


      i want to implement simmilar code inside the servlet.


      any help would be really helpful.



        • 1. Re: Accessing a Node in Servlet class
          kasq Level 3



          you can use for example following code:


          -> in the servlet class add following import and public variable:

          import org.apache.sling.jcr.api.SlingRepository;


          /** @scr.reference policy="static" */

          public SlingRepository repository;


          -> then in the doPost or doGet method you need pass in the request attribute a path to the page and use following code:


          Session session = repository.loginAdministrative(repository.getDefaultWorkspace());

          Node node = session.getRootNode();

          int pos = path.lastIndexOf('/');

          if (pos != -1) {

             StringTokenizer st = new StringTokenizer(path.substring(0, pos), "/");

             while (st.hasMoreTokens())  {

                  String token = st.nextToken();

                  if (node.hasNode("jcr:content"))  {

                         try {


                         } catch (RepositoryException e) {

                            log.warn("Error while creating intermediate node", e);






          I hope the example helps you,




          1 person found this helpful
          • 2. Re: Accessing a Node in Servlet class
            Raja_Raju_Learner Level 1



            Thanks for your help.


            i have a requirement, on clicking on the button, servlet should be invoked and it should access a node in the crx.


            Kindly provide me any sample implementation for this.


            Thanks alot.


            As i am new to CQ5 learning it.


            Thanks in advance.

            • 3. Re: Accessing a Node in Servlet class
              justin_at_adobe Adobe Employee

              It's a fairly bad practice to use loginAdministrative() in a servlet as it represents a potential security hole. (in fact, any use of loginAdministrative() should be thought through very carefully). It also means that you need to be sure you logout of the session (which the sample code above does not do).


              Instead, you can use the getResourceResolver() method of the SlingHttpServletRequest interface to get the ResourceResolver created for that particular request. This is the same object you would use in a JSP.

              1 person found this helpful
              • 4. Re: Accessing a Node in Servlet class
                Raja_Raju_Learner Level 1

                Hi Justin,


                I am finding difficulties to map the implementation with Servlet.


                I could able to store and retrieve in the component jsp. But this is the first time i am trying it from the servlet.


                is it possible to share a sample implementation, so that i can understand in better way?



                Thanks in advance.

                • 5. Re: Accessing a Node in Servlet class
                  Raja_Raju_Learner Level 1

                  Hi Justin,


                  If possible, please provide a simple implementation with both frontend component and servlet implemetnation. For example, on clicking on a button, servlet is invoked.


                  it will be really helpful, as i am digging this for more than 2 days, but not able to come out of it.


                  Kindly help me.


                  Thanks in advance.