top of page
Writer's pictureHeena

What is Sling Servlet?

Updated: May 2

  • Sling Servlet enables us to expose OSGI Service based on request - response model.

  • Every Sling Servlet must implement the Servlet interface which defines its lifecycle methods.

  • It must either extend SlingSafeMethodsServlet or SlingAllMethodsServlet

  • Servlet can either be path based(tagged to a specific paths) or resource based(tagged to specific resource types).


Lets create our first servlet:

  1. Register Servlet as an OSGI Service.

@Component(
    service={Servlet.class}, 
    property={"sling.servlet.methods=post",                                                        "sling.servlet.paths=/bin/sampleservlet"})

Available Properties:

Property key

Single Or Multivalued

Description

sling.servlet.resourceTypes

multi

​List of resourceTypes tagged to the servlet. Either this or sling.servlet.paths must be configured in the servlet else servlet is ignored. If both are configured then servlet is registered both ways. Binding servlet with resourceTypes is encouraged.

sling.servlet.resourceSuperType

single

It is the resource super type. Is sling/bundle/resource if not explicitly set.

sling.servlet.paths

multi

​List of absolute paths at which servlet is accessible.

sling.servlet.methods

multi

​The request methods supported by servlet(GET,POST,..). If '*' is passed then servlet is registered with all methods.

sling.core.servletName

single

Name of the servlet

sling.servlet.prefix

single

If the value passed is string and starts with '/' then it is appended to the servlet paths configured. If the passed value is integer then it defines the index of the search path entries of the resource resolver.

sling.servlet.selectors

multi

List of selectors enabled for the servlet

sling.servlet.extensions

multi

List of extensions supported by servlet.

sling.servlet.paths.strict

single

Setting this property to true enables strict mode where .extensions, .selectors and .methods are taken into account to select path based servlets.


2. Extend the SlingSafeMethodsServlet(if the servlet supports only GET) or SlingAllMethodsServlet

public class TestServlet extends SlingSafeMethodsServlet

3. Override required methods. For GET we will override doGet()

@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().write("Test Successful");
}


Your servlet will look like:


package com.aemblog.core.servlets;

import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(service = Servlet.class, property = { "sling.servlet.paths=" + "/bin/testservlet",
		"sling.servlet.methods=" + "GET" })

public class TestServlet extends SlingSafeMethodsServlet {
	@Override
	protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().write("Test Successful");
	}
}

4. Build and deploy the bundle. Following is the desired output:


output


That's all for today! If you've found this blog post informative or helpful, I’d greatly appreciate it if you could give it a like. It keeps me motivated 💛


Enjoying my ad-free blog? Support by buying me a coffee! I've kept this space ad-free, sponsoring it myself to maintain its purity. Your contribution would help keep the site afloat and ensure quality content. Thanks for being part of this ad-free community.

295 views

Related Posts

See All

Comments


bottom of page