Application Development (DICT312) Test 1 - August 2024 PDF
Document Details
2024
UNIVERSITY OF MPUMALANGA
Tags
Summary
This document is a past paper for an application development course (DICT312) at the University of Mpumalanga, covering topics such as monolith and microservice architectures, servlets, and their lifecycles. The paper includes multiple choice, short answer, and coding questions related to these concepts. The test was administered in August 2024.
Full Transcript
____________________________________________________________________________ Application Development (DICT312) Test 1 ------------------------- August 21st 2024 Examiner:- Dr Olalekan Samuel Ogunleye Moderator: Dr Oluwaseun Johnson Awosejo Answer All 3 Questions Duration: 70 Minutes Question 1 (16...
____________________________________________________________________________ Application Development (DICT312) Test 1 ------------------------- August 21st 2024 Examiner:- Dr Olalekan Samuel Ogunleye Moderator: Dr Oluwaseun Johnson Awosejo Answer All 3 Questions Duration: 70 Minutes Question 1 (16 Marks) 1.1) The monolith architecture has worked very well and works very well today. However, this (5) is not the case outside the cloud computing environment. Briefly discuss why monolithic architecture is inadequate for using the opportunity presented by cloud computing. Answer: The monolith is not adequate to take advantage of cloud computing because if you need to scale up this application, you'll scale all monolith. .5 That is, you will scale all business domains inside the monolith, and the benefit of paying only for the resources used will be lost. .5 1.2) Highlight the resolution of the problem identified in Question 1.1 above (2) Answer: The resolution to this problem is rethinking how to architect the applications. 1.3) Microservice architecture assists enterprise developers to take advantage of cloud (3) computing to allow scaling only the parts that need to be scaled. Discuss your understanding of Microservice Architecture. Answer: The microservice architecture breaks the monoliths into many small parts called microservices , which are generally broken according to the business domain ; that is, each microservice has the responsibility to care about one business domain. 1 1.4) Discuss how each service relates with each other in a microservice architecture (6) Answer: In a microservice architecture, each microservice has low-coupling with each other, and they are independent and deployable . This means that microservices are small applications that run without the need for another microservice to do that . Those microservices integrate with each other when needed, to provide a response to the end-user 1.5) Briefly explain the difference between monolith and microservice architectures, especially (4) in cloud computing. Answer: Monolith architecture combines all business domains in a single application, making it hard to scale specific functions independently. The microservice architecture breaks the application into smaller, independent services (microservices), each handling a particular business domain, allowing for more granular scaling and flexibility in cloud environments. Question 2 (25 Marks) 2.1) Briefly Explain how a servlet processes a request. (3) Answer: A servlet processes a request by reading data from the request, processing that data (often by accessing a database and retrieving data), and then generating a response that is sent back to the client. 2.2) List the six main HTTP methods handled by a servlet and highlight their purposes. (12) Answer: The main HTTP methods are: doGet():0.5 Handles HTTP GET requests, used to fetch data. .5 doPost():0.5 Handles HTTP POST requests, used to submit data to be processed. .5 doPut(): 0.5 Handles HTTP PUT requests, used to update data. .5 doDelete():0.5 Handles HTTP DELETE requests, used to delete data. .5 doOptions():0.5 Handles HTTP OPTIONS requests, used to describe the communication options. .5 2 doTrace():0.5 Handles HTTP TRACE requests, used for diagnostic purposes. .5 2.3) Discuss the lifecycle of a Jakarta Servlet. (9) Answer: 1. If an instance of the servlet class does not exist, then the container creates the instance. The servlet can be started either when the container is started or when the container detects that the servlet is needed. 2. The servlet is initialized by calling the init method. In case the developers want to insert some actions or behaviors at initialization time, he can override the init method. 3. After the servlet was initialized, the servlet is able to treat the requests. When a request is sent, the service method is called which is responsible for handling the request. The service method receives the ServletRequest and ServletResponse instances that represent the request and response respectively. 2.4) Explain how you would configure a servlet using annotations. (3) Answer: A servlet can be configured using the @WebServlet annotation, where you can specify parameters like the URL pattern (urlPatterns), servlet name (name), and initialization parameters (initParams). Question 3 (29 Marks) 3.1) Discuss the purpose of service() method in a servlet (4) Answer: The service() method takes responsibility to check the type of request received from the client and respond accordingly by generating a new thread or a set of threads per the requirement and implementing the operation through the following methods. 3 doGet() for GET doPut() for PUT doUpdate() for UPDATE doDelete() for DELETE. 3.2) Write the code that serves as the syntax for the service() method in servlet. (2) Answer: public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { } 3.3) Explain injection in Context Dependency Injection. (4) Answer: Injection is the process by which the CDI container provides an instance of a bean to another bean that requires it. This is typically done via field, method, or constructor injection, using the @Inject annotation. 3.4) Examine the code below and explain what the code is doing: (6) import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @ApplicationScoped public class DICTService { // This class is a bean managed by the CDI container, with Application scope } public class Client { @Inject private DICTService dictService; // CDI injects an instance of DICTService here } Answer: DICTService is annotated with @ApplicationScoped, indicating it is managed by the CDI container and there will be a single instance of this bean for the entire application's lifecycle. Client class has a field ditService annotated with @Inject. CDI will inject an instance of DICTService when Client is created, ensuring dependency management. 3.5) Examine the code below and explain what is going on in the code: (5) 4 import jakarta.enterprise.context.RequestScoped; @RequestScoped public class ProductService { public String findProduct(String id) { // Logic to find and return a product return "Product details for " + id; } } Answer: In this code below: ProductService is annotated with @RequestScoped, meaning an instance of this bean will be created for each HTTP request and destroyed when the request ends. The class contains a method findProduct which represents a business operation. 3.6) Examine the code below and explain what is going on in the code: (8) import jakarta.inject.Inject; public class ProductController { @Inject private ProductService productService; public void showProduct(String id) { String productDetails = productService.findProduct(id); System.out.println(productDetails); } } In your explanation, discuss why the ProductController Class is not explicitly annotated Answer: ProductController is a CDI-managed bean, although not explicitly annotated here (assuming it is defined in a bean-discovery-enabled archive). ProductService is injected into ProductController using @Inject. This means the CDI container will automatically instantiate ProductService (or use an existing instance if applicable) and set it on the productService field when ProductController is created. The showProduct method uses productService to get product details, demonstrating how injected beans can be utilized within application logic. END (Final Marks: 70) 5