Sling Servlet | Community
Skip to main content
AdobeID24
Level 5
July 14, 2018
Solved

Sling Servlet

  • July 14, 2018
  • 5 replies
  • 5194 views

Hi just a query....

There is two way to register the servlet in AEM:

first by path

second by resource type

I am having doubt on "by Resource type"

If I register the servlet "by path" then we can call my servlet by using Ajax at any particular event like clicking on submitting button we can get some input from end user

and we can process the user data and provide something valuable .

But in case of "by Resourcetype" when we hit the particular page and that page having resource type exactly same my servlet resource type then it would not leaving an option of event. Whenever you will hit the page your servlet would be called in the time of "sling path resolution mechanism" .

if we modify selector and extension then we can avoid that .

so my quetion is if I am having one form and user fill each and every credentails and now he hits submit button at that time I want to call my servlet and then do some modification then sending response.

Can we do it using "by Resourcetype"

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

Hi

As m.naveed​ mentioned you can register your servlet with resourcetype and unique selector and then call it during form submit by defining page URL which contains resource followed by selector and extension.

A sample way to register servlet:

@Component(immediate = true,

        service = {Servlet.class},

        property = {

                "sling.servlet.resourceTypes=<resource type>",

                "sling.servlet.extensions=html",

                "sling.servlet.selectors=formSubmit",

                "sling.servlet.methods=POST"})

A sample way to Invoke servlet on form submit:

<form id="searchform" class="navbar-search search" name="search" role="form" action="<page URL contains resource type>.formSubmit.html" method="get">

Regards,

Himanshu

5 replies

m_naveed
July 14, 2018

Hi,

You can register your servlet with the resource type of the page plus a unique selector. For more information, check the Apche Sling documentation: Apache Sling :: Servlets and Scripts

Himanshu_Singhal
Community Advisor
Himanshu_SinghalCommunity AdvisorAccepted solution
Community Advisor
July 15, 2018

Hi

As m.naveed​ mentioned you can register your servlet with resourcetype and unique selector and then call it during form submit by defining page URL which contains resource followed by selector and extension.

A sample way to register servlet:

@Component(immediate = true,

        service = {Servlet.class},

        property = {

                "sling.servlet.resourceTypes=<resource type>",

                "sling.servlet.extensions=html",

                "sling.servlet.selectors=formSubmit",

                "sling.servlet.methods=POST"})

A sample way to Invoke servlet on form submit:

<form id="searchform" class="navbar-search search" name="search" role="form" action="<page URL contains resource type>.formSubmit.html" method="get">

Regards,

Himanshu

smacdonald2008
Level 10
July 16, 2018

If you want to call a servlet when you click a button on a web page - not when you load the page - reg it by path and use AJAX to call it.

See this example - -

Scott's Digital Community: Creating a Mail List Component for the Experience Manager Toy Store Site

smacdonald2008
Level 10
July 17, 2018

NIce response!

Umesh_Thakur
Community Advisor
Community Advisor
April 4, 2019

Hi Himanshu,

Can you tell me why it is mandatory to make a sling servlet an OSGI Service unless it will throw 404 error?

I am using new style of annotation in AEM 6.4.

Thanks

Umesh Thakur