Problem without init-parameters?
Imagine you write a servlet that connects to a database. If you write the database URL, username, and password directly in your Java code, you have a big problem:
What if the database password changes? You have to find the code, change it, recompile the .java file into a .class file, and redeploy the entire application.
What if you want to use the same servlet in a different project with a different database? You can't. The code is stuck with the old settings.
This is where init parameters come in. They let you move this configuration data out of your code and into the deployment descriptor (web.xml file).
How init-parameters help when we use them in web.xml file?
They help us in a way that we don't have to find that particular part of code to change it. And also, it saves us the hectic of changing source code and recompiling that again. Anyone with .war file can change the web.xml file and use it with class file without having the need of getting .java file, and recompiling it again and again.
What's Inside a .war File 📦
A .war (Web Application Archive) file is essentially a compressed JAR file with a specific structure for web applications. When you create it, it includes:
Your compiled .class files.
The web.xml deployment descriptor.
Other resources like JSPs, static HTML, CSS, JavaScript files, and any libraries your application needs in the WEB-INF/lib folder.
The .java source files are not included in the final .war file.
The Role of the Build Tool 🛠️
When you run a command like mvn package or use the "build" function in your IDE, the build tool performs a number of steps. The compiler is a part of this process. It's smart enough to check the timestamps of your .java files and their corresponding .class files. If a .java file is newer than its .class file, it means you've made a change, and only that specific .java file (and any others that depend on it) will be recompiled.
When you only change the web.xml file, the build tool sees that none of your .java files have changed, so it skips the compilation step entirely. It just includes the updated web.xml file in the new .war archive. This is why the process is so fast.
The Person Who Receives the .war File 🧑🔧
The person who receives the .war file is a deployment engineer or a server administrator. They don't have your .java source code, so they can't recompile anything. Their job is to deploy your application. They can, however, unpack the .war file, modify the web.xml file to change a configuration parameter (like a database URL), and then repackage it or have the server read the unpacked files directly.
This is a key advantage of the .war file format and init parameters: it allows for configuration without source code access. This makes the deployment process more secure and flexible.
In a nutshell, when I am making a .war file after changes in web.xml, build tool is intelligent or smart enough or we can say programmed in such a way that it identifies that .java file was changed then it just recompiles that particular file otherwise it doesn't change that .java file and .class files remain same.
Remember, in .war we have class files, not .java files, and the person whom we give that .war file, decode that and change the web.xml file and he can use that .war file or use that web app.
So, it saves us the hectic of changing source code and recompiling that again. Anyone with .war file can change the web.xml file and use it with class file without having the need of getting .java file and recompiling it again and again.
Servlet Config:
Servlet Config is an interface of Servlet API. Web container creates servletConfig object for each servlet and passes it to every servlet. To make servlet Config, we give init parameters to each servlet in the deployment desciptor (web.xml), so when the servlet container starts running, and it gets to the point of initializing and instantiating the servlets, it reads the <init-parameter> of every servlet from web.xml and then creates ServletConfig object on the basis of those parameters, and passes that object to the servlet. So, we can access parameters at the servlet levels. So, servlet config is just a way to pass parameters to the servlet. We have already discussed the reason why we need to pass the parameters this way and not hardcode them.
Servlet Context:
Servlet Context is also an interface provided by Servlet API. The main difference between Servlet Config and Servlet Context is the scope. Servlet Config is just applied or accessible in specific servlet and Servlet Context is for application, which means its parameters and attributes are for whole application. Just like Servlet Config, we define parameters in the deployment descriptor using <context-param>, and when the container starts, it creates Servlet Config object before instantiating any servlets because we can't know which servlet will get instantiated first, that's the reason to instantiate the ServletConfig at the start.
We also need ServletContextListner for the job, because that is the place where we give the attributes to servlet Context.
Comments
Post a Comment