Servlet Container
Container in Servlet
Container provides runtime environment for Java2ee (j2ee) applications. A web container is a predefined application provided by a server, its takes care of Servlet and JSP.
In console based java applications a class which contains main method acts as a container for other classes.
Container in Servlet Example
class Vachile { void speed() { System.out.println("Speed limit is 40 km/hr"); } } class Mainclass { public static void main(String args[]) { Vachile v=new Vachile(); v.speed(); } }
Output
Speed limit is 40 km/hr
Explanation: Here Mainclass is providing run-time support for vehicle class so main class is called a container.
In GUI based applications a frame acts as a container for other awt components like button, textfield etc.
Operations of Servlet Container.
- Life Cycle Management
- Communication Support
- Multithreaded support
- Security etc.
1. Life cycle management: Servlet and JSP are dynamic resources of java based web application. The Servlet or JSP will run on a server and at server side. A container will take care about life and death of a Servlet or JSP.
A container will instantiate, Initialize, Service and destroy of a Servlet or JSP. It means life cycle will be managed by a container.
2. Communication Support: If Servlet or JSP wants to communicate with server than its need some communication logic like socket programming. Designing communication logic is increase the burden on programmers, but container act as a mediator between a server and a Servlet or JSP and provides communication between them.
3. Multithreading: A container creates a thread for each request, maintains the thread and finally destroy it whenever its work is finished.
4. Security: A programmer is not required to write security code in a Servlet/JSP. A container will automatically provide security for a Servlet/JSP.