Expand my Community achievements bar.

SOLVED

Why do we use servlets in AEM?

Avatar

Level 2

I am new to AEM. Till now I have been developing web applications using MEAN stack. The view was constructed using Angular, model is MongoDB and Controller part was handled by Node server.

 

Are servlets in AEM used to implement the controller part of web application? Like the data of a submitted form would be sent to the internal servlet which would communicate with the model to handle the data. If so, then wouldn't it be better to handle the controller logic separately as AEM should be used for the View part? Like writing the controller servlets in java and using some servlet container eg., Jboss and then submitting the form data to this REST endpoint.

 

Or are servlets only used for any front-end processing that we might require. In Angular this was achieved by the typescript code written in class file of component, a service or a directive (I can think of only these). If so, does this mean that in AEM we cannot write JavaScript or typescript code to be run at certain times and we have to use servlets only?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@uroojkhan Ok. I get your point. 

 

So we handle this in different ways.

 

1. Since nowadays its all about REST-ful service , if there is a restful POST API to submit the data to DB, we can use it directly in the FE  and call that API using AJAX. You don't have to submit the form to AEM Servlet in that case. 

 

2. If you have to connect to a third party DB , then you can post this form using AEM Servlets and in the AEM Servlet code , you can write the logic to connect to the DB and POST this information to that. 

 

 

 

View solution in original post

6 Replies

Avatar

Community Advisor

You can think of Servlets in AEM as API.Its is similar to java servlets.

 

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.



Arun Patidar

Avatar

Community Advisor

 

@uroojkhan 

 I will try to explain your questions as much as I can.

 

1."Are servlets in AEM used to implement the controller part of the web application? "

      Servlet in AEM is same as any other Servlet. It is another service used to process the request and provide the responses as required. 

 

Your understanding is correct. We can use the servlet in AEM to process a form of data or respond to some information from our repository. It depends on what kind of response you are expecting from the AEM server. 

 

2. "If so, then wouldn't it be better to handle the controller logic separately as AEM should be used for the View part? Like writing the controller servlets in java and using some servlet container"

 

     So AEM is not a FE technology like your REACT or ANGULAR, which is used only for the View Part. AEM is a Content Management System. It is built on top of a lot of Open Source Technologies. The official documentation has this beautiful explainer video of the underlying technologies here. You can read more about the same Here

 

AEM is a JAVA-based technology. AEM's technology stack has OSGI which enables us to build modular applications completely in JAVA. . The SLING helps with the content delivery part of it. The Repository ( instead of DB) is also in AEM and we call it CRX or Content Repository Xtreme, which is an implementation of JCR (Java Content Repository)

 

3. "are servlets only used for any front-end processing that we might require. "

    Simple answer. No. We mostly use servlets when we have to fetch some data from our repositories and respond to the client. 

 

4. "does this mean that in AEM we cannot write JavaScript or typescript code to be run at certain times and we have to use servlets only?"

 

   I am not a FE expert, so I cannot speak about typescripts. But AEM is a complete solution in itself. So we have the content rendered in the FE using any FE tech the business decides to work with. It works well with Angular and REACT. That being said, any page developed in AEM is like your normal HTML page; the only difference is that the content is stored in our Repository (CRX) and it is fetched using our technology stacks which include SLING, OSGI, CQSE, etc. 

  Coming back to your question, we can always write Javascript as and when required if you need to do something in the FE. 

 

Thanks

Veena ✌

Avatar

Level 2

Let me give you a particular use-case:

I am making a form in AEM which takes personal information from users. I want this user data not to be stored in the Java Content Repository but in an MSSQL database which is hosted on a separate server. I think that AEM does allow to directly setup a datasource to connect to the database but I want to write separate API's which will interact with the database server using JDBC. 

Now, will I use:

1) AEM servlets to achieve this. In this case the form data will be submitted to the internal AEM servlet which will then post it to the database server.

2) Or should I write my servlets separately from AEM and then use some servlet container like tomcat etc., Maybe this approach is better because AEM servlets should be used when we want to do some processing that is local to AEM. I'm not sure.

 

Avatar

Correct answer by
Community Advisor

@uroojkhan Ok. I get your point. 

 

So we handle this in different ways.

 

1. Since nowadays its all about REST-ful service , if there is a restful POST API to submit the data to DB, we can use it directly in the FE  and call that API using AJAX. You don't have to submit the form to AEM Servlet in that case. 

 

2. If you have to connect to a third party DB , then you can post this form using AEM Servlets and in the AEM Servlet code , you can write the logic to connect to the DB and POST this information to that. 

 

 

 

Avatar

Employee Advisor

I will try to answer all of your questions.

It's difficult to map MVC patterns directly in AEM, It is closer to MVVM where HTL acts as (View), Sling Model (View Model), and OSGI Services (Model). I would recommend going through the below if you are coming from an MVC background, this would bridge some gaps in your AEM Learning path.

There are some integrations to other Adobe products, Databases, or some third-party systems that are called through OSGi services, but the data is transmitted to the backend (java code) through servlets, you can call the servlets from your front-end code.

You could choose Javascript/Typescript for your front-end implementation, but for any data submission you may have to use Servlets. You may want to check React/Angular-based SPA archetype to see how they are wired into the AEM project.


Hope this helps.

Regards,
Nitesh

Avatar

Level 7

Servlets are used in AEM to provide a web-based solution to handle requests made by the client. They are used to process requests, process data, store data and generate responses. Servlets also form the backbone of the AEM application and are used to provide a secure and stable connection between the client and the server. They are also used to set up the environment for the processing of requests and to provide a single point of entry for all requests.