Skip to main content

Servlet Containers Explained

Servlet Containers Explained

A servlet container is a compiled, executable program responsible for loading, initializing, and executing servlets. It serves as the official Reference Implementation for Java Servlet and JavaServer Pages technologies. Developed by Sun under the Java Community Process, servlets provide a powerful, platform-independent method for server-side programming, overcoming the limitations of CGI programs.

The container handles a large number of requests and can hold many active servlets, listeners, and other components. Notably, the container and its objects are multithreaded, meaning each object must be thread-safe as multiple requests may be handled simultaneously.

Note: A servlet container can operate standalone, without a web server, or on a different host.

Types of Servlet Containers

Servlet containers can be categorized into two main types:

I. Simple Servlet Container

A simple servlet container performs the following:

  • Wait for HTTP requests.
  • Construct ServletRequest and ServletResponse objects.
  • If the request is for a static resource, invoke the process method of the StaticResourceProcessor instance, passing the ServletRequest and ServletResponse objects.
  • If the request is for a servlet, load the servlet class and invoke its service method, passing the ServletRequest and ServletResponse objects. Note that in this container, the servlet class is loaded every time it is requested.

II. Fully Functional Servlet Container

A fully functional servlet container additionally performs the following:

  • When the servlet is called for the first time, load the servlet class and call its init method (once only).
  • For each request, construct an instance of javax.servlet.ServletRequest and javax.servlet.ServletResponse.
  • Invoke the servlet's service method, passing the ServletRequest and ServletResponse objects.
  • When the servlet class is shut down, call the servlet's destroy method and unload the servlet class.

Servlet Container Functions

In general, a servlet container performs the following for each HTTP request for a servlet:

  • The servlet container loads the servlet class and calls the init method of the servlet as soon as the servlet is called for the first time.
  • The container then creates instances of javax.servlet.ServletRequest and javax.servlet.ServletResponse for each request.
  • It passes these objects by invoking the servlet's service method.
  • Finally, it calls the destroy method and unloads the servlet class when the servlet class is to be shut down.

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