Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM Servlets

Avatar

Level 10

Hi all.

 

Kindly let me know 2-3 use cases for writing AEM Servlets.

Why could these needs be not fulfilled using normal Java code?

 

Appreciate all your responses.

 

Thanks,

Rama.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@AEM_Forum 

 

Few questions for you, when you review you might be able to answer your query

 

- If a Form is submitted from UI, what would you call on backend to process that Info? UI doesn't know Java Code and Java Programs cannot be accessed from UI

 

Hint: A servlet provides a URL which UI can hit and pass the information to.

In the end Servlet is Java Code, just it provides a URL what UI can submit information to

 

- You have a Dynamic component on page, like Search. You have 100 matching results, but UI only shows 5 at a time. When you want visit next 10 results, entire page should not be refreshed. How would this page 2 be rendered? From where will it get Info? How would backend know that page 2 info is needed now?

 

Hint: When you select Page 2, an HTTP call is made to a GET servlet. Informing the servlet that page-2 is requested. It gathers that info and gives it back to UI.

Again, Servlet is JAVA CODE, but via Servlet specific annotations, it provides a URL what that UI can interact with


Aanchal Sikka

View solution in original post

6 Replies

Avatar

Community Advisor

Adobe Experience Manager (AEM) Servlets are Java classes that extend the SlingSafeMethodsServlet or SlingAllMethodsServlet classes and are used to handle HTTP requests and responses in an AEM application. While you can perform many tasks using normal Java code, AEM Servlets provide a specific set of functionalities that are tailored to the AEM environment. Here are two to three use cases where AEM Servlets are commonly used:

Handling Form Submissions and User Input:

  • Use Case: AEM websites often include forms for user interaction. AEM Servlets can be used to handle form submissions, process user input, and interact with AEM's underlying services to store or manipulate data. This can include validation, processing, and storage of form data in the AEM repository.

RESTful API Endpoints:

  • Use Case: AEM applications often need to expose RESTful APIs to support integrations with other systems or services. AEM Servlets can be used to implement custom API endpoints, handling incoming requests and providing appropriate responses. This enables external applications to interact with AEM content and services.

Dynamic Content Aggregation:

  • Scenario: You need to aggregate content from multiple sources within the AEM repository or external systems based on the parameters provided in the GET request. This aggregated content might be used to generate a composite response for display on a webpage.

 

In summary, AEM Servlets are specifically designed to address the needs of AEM applications, providing a convenient way to handle HTTP requests and interact with AEM's content repository and services.


Aanchal Sikka

Avatar

Level 10

Thanks  Aanchal for this elaborate response. This is very useful stuff.

 

Now my query is why Servlets for these use cases?

How will it help when compared with a normal Java method? (I am not even aware what to compare Servlets with).

 

Coding? Performance? etc?

 

Thanks a lot.

Rama.

 

 

Avatar

Correct answer by
Community Advisor

@AEM_Forum 

 

Few questions for you, when you review you might be able to answer your query

 

- If a Form is submitted from UI, what would you call on backend to process that Info? UI doesn't know Java Code and Java Programs cannot be accessed from UI

 

Hint: A servlet provides a URL which UI can hit and pass the information to.

In the end Servlet is Java Code, just it provides a URL what UI can submit information to

 

- You have a Dynamic component on page, like Search. You have 100 matching results, but UI only shows 5 at a time. When you want visit next 10 results, entire page should not be refreshed. How would this page 2 be rendered? From where will it get Info? How would backend know that page 2 info is needed now?

 

Hint: When you select Page 2, an HTTP call is made to a GET servlet. Informing the servlet that page-2 is requested. It gathers that info and gives it back to UI.

Again, Servlet is JAVA CODE, but via Servlet specific annotations, it provides a URL what that UI can interact with


Aanchal Sikka

Avatar

Community Advisor

I think you need to understand the basis of JavaEE vs JavaSE, I recommend reading this article to learn more: https://docs.oracle.com/javaee/6/firstcup/doc/gkhoy.html.


In short, A Java servlet is a Java programming language class that is used to extend the capabilities of servers(webservers) that host applications accessed by means of a request-response programming model. Servlets are typically used to process or store data, handle user requests, or generate dynamic content for a web application. This remains true for AEM, where a Sling Servlet is an extension of a regular Java servlet and runs over the OSGI framework through a servlet engine.

 

You can read more here:

https://javaee.github.io/tutorial/overview001.html

https://www.digitalocean.com/community/tutorials/java-web-application-tutorial-for-beginners 

https://www.javatpoint.com/servlet-tutorial 

https://sling.apache.org/documentation/the-sling-engine/servlets.html 



Esteban Bustamante

Avatar

Level 10

Hi,

 

Generally,, when we bring a concept forward, we explain the pains of existing system and cover how the new concept overcomes that.

Here for AEM Servlets, is there any such thing to compare with? 

May be a Java OSGI Method. I know that a Java Method could create HTML and return. Servlets also create HTML and return.

 

Thanks,

Rama.

Avatar

Community Advisor

Firstly, there is no specific entity called an 'AEM servlet.' AEM utilizes a stack of technologies that leverages the 'servlet engine' through the Sling framework. The accurate term to use would be 'Sling Servlet,' as it extends the functionality of a regular 'Servlet.' Understanding the broader concept of a "servlet" will provide insight into what a Sling Servlet does. It seems you may be inquiring about the basics of Java servlets and their distinctions from standard Java classes. This topic extends a bit beyond the scope of this forum. I recommend exploring the links I shared to gain a better understanding of what a Java servlet is and when/why it is useful.

 

Hope this helps



Esteban Bustamante