I have created a service called MyService and implemented it, I want to call it in my servlet so how can I do that? please help
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @ashwinikhaple
Please see below:
public class MyServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
@Reference
private transient MyService myservice;
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
myservice.anything();
}
}
https://sling.apache.org/documentation/the-sling-engine/servlets.html
Hope this helps!
Thanks!
Hi, @ashwinikhaple,
If you are using SCR annotation, you can use @Reference annotation to inject the service into the servlet.
Something similar to this.
@Reference MyService service;
You can refer to the below thread for more details.
If you are using R6 annotation, Please refer to below URL for the same.
http://www.nateyolles.com/blog/2017/05/osgi-declarative-services-annotations-in-aem
Hello @Rohit_Utreja Thank you for the reply. I have created servlet below how can I call MyService here?
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
@Component(service = Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=My Servlet",
"sling.servlet.paths="+ "/bin/demotraining/mypath"
})
public class MyServlet extends SlingSafeMethodsServlet
{
/**
*
*/
private static final long serialVersionUID = 3444770797464227100L;
MyService ms=new MyServiceImpl();
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
response.setHeader("Content-Type","text/html");
response.getWriter().print("<h1>Sample content using Sling servlet </h1>");
response.getWriter().print("<h2>My SlingServlet is registered using servlet path</h2>");
response.getWriter().close();
}
}
Views
Replies
Total Likes
Hi @ashwinikhaple,
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service = Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=My Servlet",
"sling.servlet.paths="+ "/bin/demotraining/mypath"
})
public class MyServlet extends SlingSafeMethodsServlet
{
/**
*
*/
private static final long serialVersionUID = 3444770797464227100L;
@Reference
private MyService myService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
response.setHeader("Content-Type","text/html");
response.getWriter().print("<h1>Sample content using Sling servlet </h1>");
response.getWriter().print("<h2>My SlingServlet is registered using servlet path</h2>");
response.getWriter().close();
}
}
Views
Replies
Total Likes
Now, I am trying to render values returned by methods that are in MyService, So to render those in servlet what can I use?
Below is MyService Methods code
public interface MyService
{
public String getMyFirstName();
public String getMyLastName();
public String getMyHobby();
}
Views
Replies
Total Likes
Hi @ashwinikhaple
Please see below:
public class MyServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
@Reference
private transient MyService myservice;
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
myservice.anything();
}
}
https://sling.apache.org/documentation/the-sling-engine/servlets.html
Hope this helps!
Thanks!
Please refer below community article:
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies