Expand my Community achievements bar.

SOLVED

Sling Servlet

Avatar

Level 7

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"

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

5 Replies

Avatar

Level 1

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 10

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

Avatar

Community Advisor

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