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);
}
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
Use JCR API to perform the node addition/modification.
Also you can refer below discussion:
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();
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);
Views
Replies
Total Likes
Use JCR API to perform the node addition/modification.
Also you can refer below discussion:
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();
Views
Likes
Replies