javaservletsaemaem-6

Why do we use servlets in AEM?


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?


Solution

  • In AEM, servlets are used to handle server-side processing and to interact with the model such as the AEM content repository. They are similar to the controller part of a web application in the MEAN stack.

    Servlets in AEM are typically used to handle requests from the client (such as a web browser), process the request, and then generate a response to be sent back to the client. They can be used to handle a wide range of tasks, such as retrieving content from the AEM content repository, rendering pages, or handling form submissions.

    AEM servlets can be written in Java and are usually stored in the AEM codebase. They can be accessed by making HTTP requests to a specific URL, which is mapped to the servlet.

    It is possible to use servlets in AEM to handle the controller logic of a web application, as well as any front-end processing that might be required. However, it is also possible to use other technologies and approaches to handle these tasks, such as building a separate application or service using a different framework (such as the Jmax servlet container you mentioned) and making HTTP requests to that application from AEM.

    In AEM, you can also use client-side technologies such as JavaScript or TypeScript to handle front-end processing. AEM provides a number of features and tools for building and managing front-end applications, including the Client Library system and the AEM JavaScript Use-API. These can be used to include and manage JavaScript and TypeScript code in your AEM projects.

    Nutshell, the choice of how to handle the controller and front-end processing in an AEM application will depend on your specific requirements and design considerations. It is important to carefully evaluate your options and choose the approach that best fits your needs.