Skip to main content

Servlet Lifecycle Overview

Servlet Lifecycle Overview

Servlet Lifecycle Overview

The life cycle of a servlet can be categorized into four parts:

  1. Loading and Instantiation: The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the <load-on-startup> attribute in the web.xml file. If <load-on-startup> has a positive value, the servlet is loaded with the container; otherwise, it loads when the first request is made. After loading, the container creates instances of the servlet.
  2. Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to it. The init() method must be called before the servlet can service any requests. Initialization parameters persist until the servlet is destroyed. The init() method is called only once throughout the servlet's life cycle. If the servlet is successfully loaded, it will be available for service; otherwise, the servlet container will unload it.
  3. Servicing the Request: After successful initialization, the servlet is available for service. The servlet creates separate threads for each request. The servlet container calls the service() method to handle requests. This method determines the request type and calls the appropriate method (doGet() or doPost()) to handle the request and send a response using the response object's methods.
  4. Destroying the Servlet: If the servlet is no longer needed, the servlet container calls the destroy() method. Like init(), this method is called only once throughout the servlet's life cycle. The destroy() method indicates that the servlet should no longer service requests, and it releases all associated resources. The Java Virtual Machine then claims the memory for garbage collection.
Servlet Lifecycle Diagram

Comments

Popular posts from this blog

Handling Change Events in jqGrid

Handling Change Events in jqGrid In this tutorial, we'll explore how to handle the change event in jqGrid to dynamically update another column based on the selected value. This approach is useful when you need to update related data based on user selections. Example Scenario Let's say we have a jqGrid table with two columns: Country and State. When the user selects a country, the State column should dynamically update to show the relevant states for the selected country. Implementation We'll use the dataEvents option in the colModel configuration to handle the change event. HTML Structure First, let's set up our basic HTML structure: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>H...

Handling Row Selection in jqGrid with jQuery

Handling Row Selection in jqGrid with jQuery The example below specifies the action to take when a row is selected in a jqGrid: var lastSel; jQuery("#gridid").jqGrid({ ... onSelectRow: function(id) { if(id && id !== lastSel) { jQuery('#gridid').restoreRow(lastSel); lastSel = id; } jQuery('#gridid').editRow(id, true); }, ... }); Explanation This script sets up a jqGrid with a custom action for when a row is selected. Here’s a step-by-step explanation: var lastSel; : A variable to store the last selected row ID. jQuery("#gridid").jqGrid({ ... }); : Initializes the jqGrid on the element with ID gridid . onSelectRow: function(id) { ... } : Defines a function to execute when a row is selected. ...

Persisting jqGrid State with Cookies

Persisting jqGrid State with Cookies jqGrid is an excellent jQuery plugin for displaying a grid. To enhance user experience, we added some filter possibilities and used jQuery to update the URL where data was fetched from. However, when users navigated away from the grid and returned, it would reset to its start position, losing any filtering or sorting they had set. To solve this, we needed to store the user's selections. Here are two JavaScript functions that achieve this using cookies: function saveGridToCookie(name, grid) { var gridInfo = new Object(); name = name + window.location.pathname; gridInfo.url = grid.jqGrid('getGridParam', 'url'); gridInfo.sortname = grid.jqGrid('getGridParam', 'sortname'); gridInfo.sortorder = grid.jqGrid('getGridParam', 'sortorder'); gridInfo.selrow = grid.jqGrid('getGridParam', 'selro...