Expand my Community achievements bar.

SOLVED

Calling a servlet during the page load time

Avatar

Level 7

Hello Everyone,

 

How can I call my servlet during page load time?

I know below mentioned approach.

 

 

Added this ajax call in some genral js file.
$.ajax({
                    url: "/content/data/practice/servlet/dummyServlet",
                    type: "GET",
                    data: {
                        testParam: "testVal"
                    },
                    success: function (response) {
                         console.log("Yes");
                    },
                    error: function (error) {
                        console.log("Error in sso login Url response", error);
                    },
                });
				
jcr path: /content/data/practice/servlet/dummyServlet  has sling:resourceType /apps/test/testURL

So, this called my servlet file
@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "/apps/test/testURL",
		"sling.servlet.methods=GET" }, immediate = true)
public class MyServlet extends SlingAllMethodsServlet { 

	//doGet method
}​

 

Is there any other way to call the servlet during page load time?

 

In a page I have added the component: /apps/abc/components/tagresult

I have created the below mentioned servlet with resource type as well. Will this work ?

 

@component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "abc/components/tagresult",
		"sling.servlet.methods=GET" }, immediate = true)
public class CompServlet extends SlingAllMethodsServlet {  
	//doGet method
}

 

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi Everyone,

 

Ignore my above query.

 

 

(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "abc/components/tagresult",
		"sling.servlet.methods=GET",  "sling.servlet.extensions=html" }, immediate = true)
public class CompServlet extends SlingAllMethodsServlet {  

		//do get method
}

 

"sling.servlet.extensions=html"  this resolved my issue. Now, if I open the page: http://localhost:4502/editor.html/content/abc/us/en.html this will call my servlet.

Since this page has my component.

 

Sorry for the trouble.

 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 7

Hi Everyone,

 

Ignore my above query.

 

 

(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "abc/components/tagresult",
		"sling.servlet.methods=GET",  "sling.servlet.extensions=html" }, immediate = true)
public class CompServlet extends SlingAllMethodsServlet {  

		//do get method
}

 

"sling.servlet.extensions=html"  this resolved my issue. Now, if I open the page: http://localhost:4502/editor.html/content/abc/us/en.html this will call my servlet.

Since this page has my component.

 

Sorry for the trouble.