Hi, this is a working example of of my solution here as some kind of guide. With the help of Scott's guide and this Im sure you will be on the right way:
In your JSP (or use the form component) do the equivalent of:
<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>
Here's just a mock form with some boring values to fill in...
Then in your servlet:
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 { Session session = null; 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 { session = repository.loginAdministrative(null); 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. } } protected void activate(ComponentContext ctx) { log.trace("Activating my small creation servlet"); } protected void deactivate(ComponentContext ctx) { log.trace("Deactivating my small creation servlet"); } }Here we just create a node (cq:Page) with the name that you entered in the form. It gets two nodes under neat with set values and then we add properties from the form
to these nodes. You can add more properties/values by only adding more fields and handle them and you can also skip subnodes if you want.
There are also a lot of other nice features in the JCRUtil that could help you.
In our case if we enter name = myName, email= myEmail, title=myTitle and phone = myPhone we will get the following structure
./content/myName (cq:Page)
../subproduct1 (nt:unstructured) -> with properties ( email:myEmail and title=myTitle)
../subproduct2 (nt:unstructured) -> with properties ( email:myEmail and title=myTitle)
Regards
/Johan