Showing posts with label J2EE. Show all posts
Showing posts with label J2EE. Show all posts

Saturday, February 28, 2009

What is a Servlet and a Servlet Container ?


A servlet is a Java programming language class used to dynamically process client requests and provide responses.

The javax.servlet and javax.servlet.http packages provide the interfaces and classes used to write Java Servlets. Servlet Life-Cycle methods are defined in the Servlet interface.

Servlets are controlled by the Container.

The container's role in s servlet's life

1.Communication Support
Provides a way for the servlets to talk with the Web Server.

2.Lifecycle Management
Controls the life and death of servlets. Please refer the diagram below.

The container creates a HttpServletResponse and HttpServletRequest object when a user clicks on a link to a servlet. A new thread is created for each request and these two objects are passed to the servlet's service method. The servlet figures out which method to call (doGet or doPost) based on the HTTP method sent by the client. The Response is written by the response object and it goes through the container.The service method completes and the thread dies or goes to a container managed thread pool. The objects go out of scope and will be collected by the garbage collector. The client will get the response.

3.Multithreaded Support.
A new thread is created for each servlet request.

4.Declarative Security
You can use the XML Deployment descriptor to configure security settings.

5.JSP Support
Translates JSPs to Java


Web Servers , Web Containers & Application Servers



Web Servers


Web Servers accept http requests from the clients and serve them with http responses.
A web application runs within the web container of a web server.

Eg: Apache

Web Containers

Web Containers provide an environment to run Servlets and JSPs. It implements the web component contract of the J2EE architecture which specifies a runtime environment for web components that includes security, concurrency, life-cycle management, transaction, deployment, and other services.

At the beginning tomcat was just a servlet container and was not able to handle web requests on its own. It was also much slower and was not able to support heavy traffic production environments and it did not support SSL. As a result it was used in conjunction with the Apache web server to gain higher performance and security.

Over time Tomcat was improved as a standalone web server and running it alone was faster or just as fast as running it in conjunction with Apache Web Server.The current tomcat also has full support for SSL.


Eg: Tomcat

Application Servers

Application Servers provide a J2EE platform for the development, deployment, and management of enterprise applications. It provides an applet container, a Web container, and an EJB container.

Eg: JBOSS , Websphere