How Can I call a Service in Servlet

Avatar

Avatar
Boost 1
Level 1
ashwinikhaple
Level 1

Like

1 like

Total Posts

16 posts

Correct reply

1 solution
Top badges earned
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 1
Level 1
ashwinikhaple
Level 1

Like

1 like

Total Posts

16 posts

Correct reply

1 solution
Top badges earned
Boost 1
Applaud 5
Affirm 1
View profile
ashwinikhaple
Level 1

23-03-2021

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

594 likes

Total Posts

705 posts

Correct reply

208 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

594 likes

Total Posts

705 posts

Correct reply

208 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile
asutosh_jena
Level 10

23-03-2021

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!

Answers (3)

Answers (3)

Avatar

Avatar
Ignite 1
MVP
Ankur_Khare
MVP

Likes

160 likes

Total Posts

370 posts

Correct reply

58 solutions
Top badges earned
Ignite 1
Validate 10
Validate 1
Give Back 50
Give Back 5
View profile

Avatar
Ignite 1
MVP
Ankur_Khare
MVP

Likes

160 likes

Total Posts

370 posts

Correct reply

58 solutions
Top badges earned
Ignite 1
Validate 10
Validate 1
Give Back 50
Give Back 5
View profile
Ankur_Khare
MVP

23-03-2021

Kindly use like below-

 

@reference
private transient ApiService apiService;

 

 

Avatar

Avatar
Validate 1
Level 6
Bhuwan_B
Level 6

Likes

137 likes

Total Posts

127 posts

Correct reply

50 solutions
Top badges earned
Validate 1
Affirm 50
Give Back 5
Boost 100
Give Back 3
View profile

Avatar
Validate 1
Level 6
Bhuwan_B
Level 6

Likes

137 likes

Total Posts

127 posts

Correct reply

50 solutions
Top badges earned
Validate 1
Affirm 50
Give Back 5
Boost 100
Give Back 3
View profile
Bhuwan_B
Level 6

23-03-2021

Avatar

Avatar
Establish
Level 4
Rohit_Utreja
Level 4

Likes

44 likes

Total Posts

45 posts

Correct reply

15 solutions
Top badges earned
Establish
Give Back 3
Give Back
Boost 5
Boost 3
View profile

Avatar
Establish
Level 4
Rohit_Utreja
Level 4

Likes

44 likes

Total Posts

45 posts

Correct reply

15 solutions
Top badges earned
Establish
Give Back 3
Give Back
Boost 5
Boost 3
View profile
Rohit_Utreja
Level 4

23-03-2021

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.

https://stackoverflow.com/questions/56732548/aem-6-4-x-osgi-servlet-how-to-access-an-osgi-service-fr...

 

 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

 

ashwinikhaple

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();
}
}

Rohit_Utreja

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();

}

}

ashwinikhaple

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();
}