Saturday, May 2, 2015

Web Container


Tomcat is a web container, when a request is made from Client to web server, it passes the request to web container and it’s web container job to find the correct resource to handle the request (servlet or JSP) and then use the response from the resource to generate the response and provide it to web server. Then web server sends the response back to the client.
When web container gets the request and if it’s for servlet then container creates two Objects
HTTPServletRequest and HTTPServletResponse. Then it finds the correct servlet based on the URL and creates a thread for the request. Then it invokes the servlet service() method and based on the HTTP method service() method invokes doGet() or doPost() methods. Servlet methods generate the dynamic page and write it to response. Once servlet thread is complete, container converts the response to HTTP response and send it back to client.

Some of the important work done by web container are:
• Communication Support – Container provides easy way of communication between web server
and the servlets and JSPs. Because of container, we don’t need to build a server socket to listen
for any request from web server, parse the request and generate response. All these important and
complex tasks are done by container and all we need to focus is on our business logic for our
applications.
• Lifecycle and Resource Management – Container takes care of managing the life cycle of
servlet. Container takes care of loading the servlets into memory, initializing servlets, invoking
servlet methods and destroying them. Container also provides utility like JNDI for resource
pooling and management.
• Multithreading Support – Container creates new thread for every request to the servlet and
when it’s processed the thread dies. So servlets are not initialized for each request and saves time
and memory.
• JSP Support – JSPs doesn’t look like normal java classes and web container provides support for
JSP. Every JSP in the application is compiled by container and converted to Servlet and then
container manages them like other servlets.


No comments:

Post a Comment

Contact Form

Name

Email *

Message *