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
ServletRequestandServletResponseobjects. - If the request is for a static resource, invoke the
processmethod of theStaticResourceProcessorinstance, passing theServletRequestandServletResponseobjects. - If the request is for a servlet, load the servlet class and invoke its
servicemethod, passing theServletRequestandServletResponseobjects. 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
initmethod (once only). - For each request, construct an instance of
javax.servlet.ServletRequestandjavax.servlet.ServletResponse. - Invoke the servlet's
servicemethod, passing theServletRequestandServletResponseobjects. - When the servlet class is shut down, call the servlet's
destroymethod 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
initmethod of the servlet as soon as the servlet is called for the first time. - The container then creates instances of
javax.servlet.ServletRequestandjavax.servlet.ServletResponsefor each request. - It passes these objects by invoking the servlet's
servicemethod. - Finally, it calls the
destroymethod and unloads the servlet class when the servlet class is to be shut down.
Comments
Post a Comment