CQ & Sling servlets: resourceType

Last time we see how to use Sling servlets with old legacy URL. Today we’ll see how to create Sling Servlets in the Sling-way: using resourceType.

Scenario

We want a component that can be dragged into a parsys. With a form that submit data to a sling servlet. Then the servlet will do the operations required.

Process

Let’s create the component following the CQ guidelines for making it drag-droppable into a parsys (allowedParents): geometrixx/components/test003. The test003.jsp will look like the following:

<%@include file="/libs/foundation/global.jsp"%>
<form name="frm" method="post" action="<c:out value="${resource.path}" />.c">
    <input type="submit" />
</form>

This JSP will answer at a GET http call showing the form. In order to use, create a content page and drag the component into a parsys.

Now it’s time to create the servlet. As usual you’ll have to register it using some felix annotations. The following servlet will be used when posting (HTTP POST) to a resource of type geometrixx/components/test003 with an extension of “c”.

package com.samples.sling;

import java.io.IOException;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @scr.component immediate="true" metatype="false"
 * @scr.service interface="javax.servlet.Servlet"
 * @scr.property name="sling.servlet.methods" values.0="POST"
 * @scr.property name="sling.servlet.resourceTypes" values.0="geometrixx/components/test003"
 * @scr.property name="sling.servlet.extensions" values.0="c"
 */

public class ResourceTypePostServlet extends SlingAllMethodsServlet {
   private static final long serialVersionUID = 8795673847499208743L;
   private final static Logger logger = LoggerFactory.getLogger(ResourceTypePostServlet.class);

   @Override
   protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
      logger.debug("ResourceTypePostServlet::doPost()");
      response.setContentType("text/plain");
      response.getOutputStream().print("Hello ResourceTypePostServlet World!");
   }
}

We used the sling.servlet.methods to specify which HTTP method the servlet listen to, the sling.servlet.resourceTypes to specify all the resource types is should listen to and the .extensions if you wish to specify an extension.

Remember that if you specify a sling.servlet.paths, resourceTypes, extensions and selectors will be ignored.

Now build the bundle and refresh the previously created content page (should not necessary actually). If you click on the “submit” button you’ll see the Hello World.

References:

Advertisements

15 thoughts on “CQ & Sling servlets: resourceType

  1. ievgen says:

    Hi! Thank you for this tutorial. That’s great that there are people who do this.
    Do you know if it is possible to set sling.servlet.resourceTypes to some relative path?
    I have the following structure of a component in cq5:
    /apps/TEST/components/DatabaseConnection (DatabaseConnection is a component name)
    /apps/TEST/components/DatabaseConnection/src/ServletDatabaseConnection (This is the serlvet. sling.servlet.resourceTypes is set to TEST/components/DatabaseConnection)

    It works this way. but what if I want to move my component DatabaseConnection to some other folder? My servlet will not work any way, I will have to change the sling.servlet.resourceTypes. Sorry for disturbing you. I hope I was clear enough on my question.

    Thanks!

  2. Hi,

    I want to post form value from one page to another page without storing values in jcr. Could you Please give me a good example code.

    requirement is:

    page1: has one form(Method:post) with some input field.

    page2: get the values sent from page1 and do some calculation

    Quick response will really helpful. Thank you.

      • i have tried to submit the form and it was successful but form values are not submitted to other page. im getting null values from page1 to page1.

        Example:

        Form:
        <form method="POST" action=".POST.html” enctype=”multipart/form-data”>

        Servlet:

        @Component(immediate = true, metatype = false, label = “QuestionnaireServlet”)
        @Service
        @Properties(value = {
        @org.apache.felix.scr.annotations.Property(name = “sling.servlet.methods”, value = { “POST” }),
        @org.apache.felix.scr.annotations.Property(name = “sling.servlet.resourceTypes”, value = { “sling/servlet/default” }),
        @org.apache.felix.scr.annotations.Property(name = “sling.servlet.selectors”, value = { “POST” }),
        @org.apache.felix.scr.annotations.Property(name = “sling.servlet.extensions”, value = { “html” })
        })
        public class QuestionnaireServlet extends SlingAllMethodsServlet implements
        OptingServlet {

        protected void doPost(SlingHttpServletRequest request,
        SlingHttpServletResponse response) throws ServletException,
        IOException {
        //it seems never call this function.
        }
        }
        is there anything wrong in this?

    • Values is a multi-value property. If you register it with one resourceType only you could use the “values” only.

      Btw doclet are deprecated. Please use javadoc instead.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s