Building Blocks of Web Application Development in Java
In our last Episode 1 (https://www.garudax.id/pulse/introduction-web-application-development-osman-mohammed/) we learned about Intro to Web Application Development. In this episode, we are going to learn about the below things.
1) What is a Java Application Server?
2) What is a Java Servlet Life Cycle?
So let's get started. Just a heads up, this section might be a little overwhelming for some of you folks.
1) What is a Java Application Server?
- To fully understand What Java Application Server is? we need to understand what problems does it solves?
- To answer the above question, first, we need to understand the most fundamental feature of a web application (apart from the application logic) is to process HTTP/HTTPS requests and respond to them, it also needs to manage concurrent requests, managing many client connections in parallel. So to achieve this most fundamental feature we need to write a simple software program which listens to HTTP/HTTPS request on one of its hosted operating system ports, parses the HTTP/HTTPS request(which is in binary format), performs some function as per the incoming HTTP/HTTPS request and sends the appropriate HTTP/HTTPS response (which is also in binary format) back to the client, you might have heard about this program it is also known as Web Server, while building this program we need to handle many concerns like What if our program receives multiple requests, and these incoming requests might have different requirements like one request might need a specific dataset and others might need a different dataset, and some requests might need datasets that our program doesn't know of, so in order to handle many clients connecting at once our program needs to execute in parallel for each client request, This means that it needs some kind of thread management and message dispatch system. Furthermore, if our program needs to respond differently to different requests, it needs a way to filter which requests go to which parts of our program.
- Enter Java Application Server, The Java application server provides solutions to both of these problems by way of its pluggable architecture. The application server doesn't actually provide any application logic itself. Instead, it provides multithreading, requests filtering, and shared resource management to any applications that are deployed to it. An application can have many endpoints that receive requests filtered by the server and can access resources like thread pools and database connections that are shared by the server with each of its deployed applications. That means that even apps developed by different teams can be plugged into the same application architecture and use the same common resources, saving time and provision resources like servers and load balancers. So when you're developing a web application for a Java application server, remember that it lives as a module within the server with potentially many entry points that receive requests matching their filters, it has access to resources managed by the server like database connections and thread pools, and it might live alongside other apps deployed to the same server.
2) What is a Java Servlet Life Cycle?
Now that we understand what an application server is, it's time to learn how to build applications for one. The core of a Java web application is the servlets. Servlets are the interface between our Web Application and the Java app server. By implementing one or more servlets in your application, you provide endpoints for the server to send incoming requests to. You can also configure your application to map certain web URLs to specific servlets which the server will enforce. Let's take a look at how an application server loads and manages the lifecycle of a servlet.
- First, let's see how the server finds the servlets in an application. When we first deploy our application to an app server, the server scans our application files for a web.XML file. We can use this file to tell the server which servlets are in our application and which URLs they map to. Using this information, the server uses Java's reflection API to load the relevant servlet classes and prepares to manage them.
- Next, let's see how the app server manages the life cycle of registered servlets. Like we said before, first, the app server uses Java's reflection API to programmatically instantiate the Servlets class. This instance of our Servlets is what the app server will use to process incoming requests, and it will remain in memory until the app server shuts it down or it is instructed to unload it. Then the app server calls our servlet's .init method, which triggers any initialization logic we gave the servlet's. Once this method returns, our servlet is ready to process incoming requests. So for an incoming request, the app server calls our servlet's .service method, which triggers our servlet's request handling logic. As long as our servlet is still active, each incoming request will be handled in this way. When the servlet is finally ready to be unloaded, for example, because of an app server shutdown or because the application was removed from the app server, the app server calls our servlet's .destroy method, which triggers any cleanup logic we defined for our servlet. So that's the lifecycle of a single servlet in an application.
- The application server manages the entire lifecycle of our application, creating servlets automatically in calling their methods in response to incoming requests and lifecycle events. Each servlet can be mapped to respond to different requests. So each distinct entry point to the application can be represented by a single servlet.
- Now that we understand this servlet lifecycle a little better, let's review the essential elements. Every servlet has to implement the service method. When the app server receives a request that it can match to a servlet, it calls the service method of the appropriate servlet passing any relevant request data as an argument. The servlet's implementation of this method is responsible for constructing the response to the request, which the app server will then forward to the client. If a servlet needs special initialization or tear-down logic, it can implement the init and destroy methods. Init is called after first creating the servlet and can perform custom initialization logic. Destroy, on the other hand, is called before removing the Servlet and should be written to clean up any heavyweight resources like file handles and database connections. Finally, in order to tell the app server which requests should be routed to which servlets in the application, a web.xmlfile needs to be included in our project code. With entries that identify servlet classes and their URL mappings.
I would like to conclude episode 2 of our Season 1 here, stay tuned for episode 3.
References
Disclaimer
This article is governed by the "Fair Use" doctrine and is only for the purpose such as criticism, comment & teaching.
- Note:- This is a living article.