Expand my Community achievements bar.

SOLVED

How to create content node from Java Servlet on AEM 6.3 ?

Avatar

Level 4

Hi All,

How can I create a new content node (or edit an existing content node) from a Java Servlet on AEM 6.3 ?

I have attached below the Servlet source code.

Thank you for your time and best regards.

--------

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.servlets.HttpConstants;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import javax.jcr.Node;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import java.io.IOException;

import java.io.PrintWriter;

@Component(service=Servlet.class,

           property={

                   Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

                   "sling.servlet.methods=" + HttpConstants.METHOD_GET,

                   "sling.servlet.paths="+ "/bin/TestCreateNodeServlet"

           })

public class TestCreateNodeServlet extends SlingSafeMethodsServlet

{

    private ResourceResolver resourceResolver;

    private Resource resource;

    private PrintWriter responseWriter;

    @Override

    protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException

    {

        try

        {

            response.setContentType("text/plain");

            resourceResolver = request.getResourceResolver();

            resource = request.getResource(); 

            responseWriter = response.getWriter();

           

            write("Version 2018-Sept-07.1");

            write(resourceResolver.toString());

            write(resource.toString());

           

            Node node = resource.adaptTo(Node.class);

            write(node.toString());

           

            //resourceResolver.create(arg0, arg1, arg2)

        }

        catch (final Exception e)

        {

            write("ERROR: " + e.getMessage() + " , " + e.getClass());

        }

    }

    /**

     * Write message to Response Writer.

     */

    private void write(final String message)

    {

        if (responseWriter != null)

        {

            responseWriter.println("TestCreateNodeServlet: " + message);

        }

    }

}

1 Accepted Solution

Avatar

Correct answer by
Level 2

Use JCR API to perform the node addition/modification.

Also you can refer below discussion:

write to JCR from JSON data

Sample :

Resource resource = request.getResourceResolver().getResource("nodePath");

Session session = resource.adaptTo(Session.class);

Node node = session.getNode(resource.getPath());

            node.addNode("test");

            session.save();

            session.logout();

View solution in original post

2 Replies

Avatar

Community Advisor

Hi,

Please use JCR NODE API to create/modify nodes using servlet.

Node (Content Repository for Java Technology API Version 2.0)

You can adapt to Node from request like below:

Resource resource = request.getResourceResolver().getResource("nodePath"); 

Node node = resource.adaptTo(Node.class);



Arun Patidar

Avatar

Correct answer by
Level 2

Use JCR API to perform the node addition/modification.

Also you can refer below discussion:

write to JCR from JSON data

Sample :

Resource resource = request.getResourceResolver().getResource("nodePath");

Session session = resource.adaptTo(Session.class);

Node node = session.getNode(resource.getPath());

            node.addNode("test");

            session.save();

            session.logout();