Servlet in AEM
What/Why servlet
Creating/registering a servlet in aem
What/why servlet:
Servlet is a JAVA class used to extend the capabilities of server using request/response programming model.
Creating/registering a servlet in aem:
Servlet can be registered as OSGI service using SCR annotations or using OSGI declarative services
Here I am registering servlet with path /bin/learnAEM if you are registering your servlet with some different path like /bin/projectName/TestServletor /lib/ProjectName/TestServlet you should register you servlet in Apache Sling Servlet/Script Resolver and Error Handler Configuration.
Register servlet by resource :
We have two type of servlet
Creating/registering a servlet in aem
What/why servlet:
Servlet is a JAVA class used to extend the capabilities of server using request/response programming model.
Creating/registering a servlet in aem:
Servlet can be registered as OSGI service using SCR annotations or using OSGI declarative services
- Register servlet using SCR annotation
@SlingServlet(paths="/bin/learnAEM", methods = "GET")
public class TestServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/json");
PrintWriter out = response.getWriter();
out.println("test data");
out.close();
}
}
This is the sample example of servlet here we used the @SlingServlet scr annotation to make java class as a sling servlet.public class TestServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/json");
PrintWriter out = response.getWriter();
out.println("test data");
out.close();
}
}
Here I am registering servlet with path /bin/learnAEM if you are registering your servlet with some different path like /bin/projectName/TestServletor /lib/ProjectName/TestServlet you should register you servlet in Apache Sling Servlet/Script Resolver and Error Handler Configuration.
- Register servlet using OSGI declarative services
@Component
@Service(Servlet.class)
@Properties({
@Property(name="sling.servlet.paths",value="/bin/learnAEM"),
@Property(name="sling.servlet.methods",value="GET"),
@Property(name="sling.servlet.extensions",value="HTML")
})
public class TestServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/json");
PrintWriter out = response.getWriter();
out.println("test data");
out.close();
}
}
Using these two ways we make servlet as a OSGI service now we will see how to register service in AEM@Service(Servlet.class)
@Properties({
@Property(name="sling.servlet.paths",value="/bin/learnAEM"),
@Property(name="sling.servlet.methods",value="GET"),
@Property(name="sling.servlet.extensions",value="HTML")
})
public class TestServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/json");
PrintWriter out = response.getWriter();
out.println("test data");
out.close();
}
}
- Register servlet by path
- Register servlet by resource
Register servlet by resource :
@SlingServlet(name="TestServlet",
resourceTypes="/blog/component/servletExample",
extensions = "pdf",
paths="/bin/learnAEM",
methods = "GET",
metatype=true)
@Properties({
@Property(name="service.description",value="test servlet", propertyPrivate=false),
@Property(name="service.pid", value="com.day.servlets.TestServlet",propertyPrivate=false),
})
public class TestServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/json");
response.getWriter().print("test Data");
response.getWriter().close();
}
}
resourceTypes="/blog/component/servletExample",
extensions = "pdf",
paths="/bin/learnAEM",
methods = "GET",
metatype=true)
@Properties({
@Property(name="service.description",value="test servlet", propertyPrivate=false),
@Property(name="service.pid", value="com.day.servlets.TestServlet",propertyPrivate=false),
})
public class TestServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2598426539166789515L;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/json");
response.getWriter().print("test Data");
response.getWriter().close();
}
}
when you are registering a sevlet using resourceTypes servlet will need additional configuration property extension(sling.servlet.extension)
This configuration will let your servlet run in context of a resource instead of global/by default
We have two type of servlet
- SlingSafeMethodServlet : Helper base class for read-only servlet in sling. It support GET, HEAD, OPTIONS, TRACE, GENERIC, SERVLETINFO etc. methods.
- SlingAllMethodServlet : Helper base class for data modifying servlet in sling. It support SlingSafeMethodServlet methods and POST, PUT, DELETE methods.
Comments
Post a Comment