Skip to main content

Posts

Showing posts from August, 2025

Events and Listeners | Servlet Context Listener

  Event: Event is an entity, that notifies someone (listener) when something happens. There are few kinds of events: ServletContextEvent (notifies when the webapp is initialized / when the servlet Context is made) ServletRequestEvent (notifies the listener each time the request comes) Listener: Listener is an entity that is notified each time an event occurs. It is like a piece of code which runs when an event happens. In other words, when we need to associate some part of code to some event, and we want that code to run only when that specific event occurs, then we use event listeners. Types: Few event listeners are mentioned below: ServletContextListener        ( Initializes resources and performs setup when the web application starts and shuts down). HttpSessionListener            ( Notifies when a user's session is created or destroyed). ServletRequestListener         ( Notifies when an HTTP request ...

HTTP Requests and Responses

Requests: There are two types of requests: Idempotent Non-Idempotent Idempotent: The requests which produce same effect (do not affect the data) on repeated requests, are idempotent. In other words, Same request can be made twice with no negative consequences to the server. GET DELETE PUT  These are idempotent, because once deleted or updated or read, they do not change anything on the server. Non-Idempotent: The request whose changes produces different effect every time,  is non-idempotent. This means every time we send request to the server; the database of server is updated on each request. Post Patch These are idempotent and needs to be managed carefully to check whether this request is previously fulfilled or not.  For Example: If we place an order using our bank account details, order would be placed. If the order is placed again at the same type (because we didn't get any response back, we place the same order again, then it would again place that order, so we need...

Servlet Context and Servlet Config

  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 ne...

Java Servlets

  According to me its just a translator, who manages the HTTP requests and handle them to the appropriate function of the API. So then, API turns out to be a term or set of those functions which are kept public. Now let us see in little bit of detail what servlets are: For that we need to see few terms: Static Web Pages: Static web pages are the pages, which are already built, and whose content and design always remain the same.  For Example: About us page of some website. Contact info page of some website Dynamic Web Pages: Dynamic pages are not already defined. They are dependent on search queries. For Example: If we search top 5 books, and click on search button, result would be different. If we search top 5 countries and enter search, result would be different.  So, the search result for every search is different. Working: Now if we have to search static page let's say the first page of any website (which is same for every use in every country) then the process is sim...