How to get request parameter of SlingServlet in OSGi Service ? | Community
Skip to main content
Suraj_Kamdi
Community Advisor
Community Advisor
October 30, 2018

How to get request parameter of SlingServlet in OSGi Service ?

  • October 30, 2018
  • 1 reply
  • 12077 views

I want to fetch the properties in OSgi Service like userName , which is set in request parameter in a my custom SlingServlet ? Anyone has an idea about how to achieve this task ?

I have one RegisterUserSlingServlet class and CustomMailService as OSGi service. I want get some request parameters properties inside my CustomMailService.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

chandu_t
Level 3
October 30, 2018

Assuming you call CustomMailService in RegisterUserSlingServlet. You get request parameters in servlet by request.getParameter("key"). You can pass this parameters to the service, while invoking service method.

Feike_Visser1
Adobe Employee
Adobe Employee
October 30, 2018

Indeed, just pass in the request object or individual values

Suraj_Kamdi
Community Advisor
Community Advisor
October 30, 2018

Do you have any code to share?


Feike Visser

Servlet Code

@Component(service = Servlet.class,

    property = {Constants.SERVICE_DESCRIPTION + "= Create Profile Servlet",

        ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_POST,

        ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + CreateProfile.RESOURCE_TYPE})

public class CreateProfile extends SlingAllMethodsServlet {

  public static final String RESOURCE_TYPE = "components/general/profile";

  private static final String targetPageUrl = "/content/smaple.html";

  @Reference

  private transient AccountManagementService accountManagementService;

  @Override

  protected void doPost(SlingHttpServletRequest slingRequest,

      SlingHttpServletResponse slingResponse) throws ServletException, IOException {

     //Want to pass this targetUrl into Service class

    slingRequest.setAttribute(targetPageUrl, targetPageUrl );

  }

}

Service Class:

@Component(

    name = "Mail Service",

    property = {

        "label = Mail Service",

        "description = Mail Service",

        Constants.SERVICE_RANKING + ": Integer = 10000"},

    service = MailService.class, immediate = true

)

public class ]MailService implements MessageGateway<Email>, MailService {

  /**

   * Getting reference for default mail service.

   */

  @Reference(

      target = "(service.pid=com.day.cq.mailer.DefaultMailService)",

      cardinality = ReferenceCardinality.MANDATORY,

      policy = ReferencePolicy.DYNAMIC

  )

  private volatile MailService defaultMailService;

  @Activate

  protected void activate(ComponentContext componentContext) {

    log.info("Activated Fiserv Default Mail Service");

  }

  @Override

  public boolean handles(Class<? extends Email> type) {

    return true;

  }

  @Override

  public void send(Email email) throws MailingException {

     //Here I want to Fetch the Request Parameter From Servlet Class.

  }

  @Override

  public void sendEmail(Email email) throws EmailException {

    send(email);

  }

}