How to get values form param and save form data as node in crx using servlet. I newbie to aem
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Dilli_babu_22 ,
In AEM data is stored in JCR in the forms of nodes and properties, So to save data in JCR, you need to create nodes or/and properties. In your servlet get the data and create node/property using JCR Node.
Please check below snippet, it is just for demonstration.
<form id="submitForm" method="post" action="/apps/my/servlets/path/create">
<input type="text" name="name" value="" />
<input type="text" name="email" value="" />
<input type="text" name="title" value="" />
<input type="text" name="phone" value="" />
<input type="submit" value="Submit"/>
</form>
package com.test.servlet;
import com.day.cq.commons.jcr.JcrUtil;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
@Component(immediate = true, metatype = true, name = "com.test.CreateServlet", label = "Create Servlet", description = "Test creation servlet")
@SlingServlet(methods = { "POST" }, paths = "/apps/my/servlets/path/create", generateComponent = false)
@Properties({ @Property(name = "service.description", value = "CreateServlet"), @Property(name = "service.vendor", value = "My Vendor") })
@SuppressWarnings({ "serial", "unused" })
public class CreateServlet extends SlingAllMethodsServlet {
private static final String CREATE_PATH = "/content"; /// Set the base path here
private static final Logger log = LoggerFactory.getLogger(CreateServlet.class);
@Reference private SlingRepository repository;
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
//We are not using this atm.. but you could
}
@Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resourceResolver = request.getResourceResolver();
Session session = resourceResolver.adaptTo(Session.class);
String name = request.getParameter("name");
String email = request.getParameter("email");
String title = request.getParameter("title");
String phone = request.getParameter("phone");
PrintWriter out = response.getWriter();
try {
Node firstProduct = JcrUtil.createPath(CREATE_PATH + "/" + name , "cq:Page", session);
Node firstSubProduct = JcrUtil.createPath(firstProduct.getPath() + "/subproduct1", "nt:unstructured", session);
Node secondSubProduct = JcrUtil.createPath(firstProduct.getPath() + "/subproduct2", "nt:unstructured", session);
firstSubProduct.setProperty("email", email);
firstSubProduct.setProperty("title", title);
secondSubProduct.setProperty("email", email);
secondSubProduct.setProperty("title", title);
session.save();
out.println("That went well...");
out.flush();
out.close();
}
catch (RepositoryException e) {
out.println("That went not so well...");
out.flush();
out.close();
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
Hope that helps!
Regards,
Santosh
Hi @Dilli_babu_22 ,
In AEM data is stored in JCR in the forms of nodes and properties, So to save data in JCR, you need to create nodes or/and properties. In your servlet get the data and create node/property using JCR Node.
Please check below snippet, it is just for demonstration.
<form id="submitForm" method="post" action="/apps/my/servlets/path/create">
<input type="text" name="name" value="" />
<input type="text" name="email" value="" />
<input type="text" name="title" value="" />
<input type="text" name="phone" value="" />
<input type="submit" value="Submit"/>
</form>
package com.test.servlet;
import com.day.cq.commons.jcr.JcrUtil;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
@Component(immediate = true, metatype = true, name = "com.test.CreateServlet", label = "Create Servlet", description = "Test creation servlet")
@SlingServlet(methods = { "POST" }, paths = "/apps/my/servlets/path/create", generateComponent = false)
@Properties({ @Property(name = "service.description", value = "CreateServlet"), @Property(name = "service.vendor", value = "My Vendor") })
@SuppressWarnings({ "serial", "unused" })
public class CreateServlet extends SlingAllMethodsServlet {
private static final String CREATE_PATH = "/content"; /// Set the base path here
private static final Logger log = LoggerFactory.getLogger(CreateServlet.class);
@Reference private SlingRepository repository;
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
//We are not using this atm.. but you could
}
@Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resourceResolver = request.getResourceResolver();
Session session = resourceResolver.adaptTo(Session.class);
String name = request.getParameter("name");
String email = request.getParameter("email");
String title = request.getParameter("title");
String phone = request.getParameter("phone");
PrintWriter out = response.getWriter();
try {
Node firstProduct = JcrUtil.createPath(CREATE_PATH + "/" + name , "cq:Page", session);
Node firstSubProduct = JcrUtil.createPath(firstProduct.getPath() + "/subproduct1", "nt:unstructured", session);
Node secondSubProduct = JcrUtil.createPath(firstProduct.getPath() + "/subproduct2", "nt:unstructured", session);
firstSubProduct.setProperty("email", email);
firstSubProduct.setProperty("title", title);
secondSubProduct.setProperty("email", email);
secondSubProduct.setProperty("title", title);
session.save();
out.println("That went well...");
out.flush();
out.close();
}
catch (RepositoryException e) {
out.println("That went not so well...");
out.flush();
out.close();
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
Hope that helps!
Regards,
Santosh
Thanks bro @SantoshSai
Where does the servlet code go?
Views
Replies
Total Likes
@Dilli_babu_22 I assume you are learning AEM and this might be some task given to you. Please refer @SantoshSai response. You should be good to go . But please do try to debug , add logs and understand each line in the Java code and understand what each API methods does. It will help you in the long run.
Thanks
Veena ✌
Yes mam, I am new to aem. Now only learning new topics mam. Thanks mam for your suggestion