Sling Servlet | Community
Skip to main content
AdobeID24
Level 5
Jul 14, 2018 2:39 PM UTC
Solved

Sling Servlet

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.
Himanshu_Singhal
Himanshu_SinghalAccepted solution
New Participant
Jul 15, 2018 10:48 AM UTC

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

Umesh_Thakur
New Participant
Apr 4, 2019 10:59 AM UTC

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

Translate
smacdonald2008
Level 10
Jul 17, 2018 12:26 AM UTC

NIce response!

Translate
smacdonald2008
Level 10
Jul 16, 2018 3:34 PM UTC

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

Translate
Himanshu_Singhal
Himanshu_SinghalAccepted solution
New Participant
Jul 15, 2018 10:48 AM UTC

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

Translate
m_naveed
New Participant
Jul 14, 2018 9:04 PM UTC

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

Translate