Jsp and Servlet Tutorials - Servlet Architecture and Use of Web Container

Welcome to Second part of our jsp-servlet tutorial series, so far we have discussed What is request-response model and a quick introduction to Servlet. In this particular blog we will try to cover Architecture of Servlet and a little about Web Containers and their usage in Servlet world.

Archetecture of servlets
Till now we have a brief introduction about servlet need and usage now the question arises that what exactly the servlet is ? and what is servlet architecture


In simple words we can say that servlet is nothing but a java program, rather we should say it is a special java program, why this is special java program ?, because this does not have a main method instead it has some call back methods so called get or post method as discussed before.

Before we come to know more about get and post method and their implementations, lets first see the request-response cycle in servlets environment. We already knows that to serve dynamic pages a server needs servlets now the question arises, how the server communicates with servlets ? the answer to this question is Web Container.

What does the web container is exectly ?
A web container so called a servlet engine in one which helps the server to communicate with servlet and servlets takes birth and died in a web container, actually a web container is much more than it.
Whenever a request comes from client to server the server sends that request to web conainer and web container invokes either a get or a post method written on servlet, based on which method is being requested in user's request. Now the code written inside that servlet's method body is executed to make response ready. This response is send back to server and finally server sends a dynamic resource back to client.

How does the container handles the request ?
Till now we came to know that whenever a http request for dynamic resource came to the server, the server send that http request to container and now the container is responsible for invocking the right method of right servlet by sending this http request to servlet. And as we already knows that servlet is a java programme, now the question arises that how a java programme understand http request ?, because java understand objects only.

Now the actual function of web container comes into picture, the container converts the http request into valid request and response objects to communicate to servlet. The request object is responsible to send request to servlet programme and run right get or post method accordingly, response object is responsible for getting back the response from servlet to container.
Now a number of questions arises here, what happens when there are a number of requests for a single servlet ? does the same request and response ojects are being used for every request ? what happens to those request and response objects when response is getted back by container ?
The answer to these questions is that,

Every time a new request came to the container it generates a new thread per request and new request and response object for every new request. When the response is got back by the container the request/response objects are destroyed by the container itself. Hence container plays a very important role in servlet life cycle.

Role of web Container 
A web container can have a number of jsp and servlet inside it, it does a number of things by itself so that the developer can concentrate on business logic rather than other low level functions. Some of the main roles of web container are follows.

1- Communication Support : Everything that happens to a servlet the container is responsible for that. It provide an communicative interface between server and the servlets.

2- Lifecycle Management Support :
Servlet have a number of methods to initialize it, provide services and destroy it. And all those methods are invoked by the container from the birth to the death of an servlet.

3- Multi-threading support :
For every request that comes in for the servlet the conatiner creates a saperated thread and execute service method as per client request.

4- Security Handling :
As the requests are always coming through the container, we can put security constarints in container to stop a invalid request being processed.

5- JSP Support

Now the question arises how does the container knows which servlet is being requested by the client, the answer to this question is web.xml file that resides there on server and it have valid mappings from request to servlet. This web.xml looks something like this:

Every servlet in a web application must have an entry into this file withing two tags <servlet> and <servlet-mapping>. Now lets see these fields in details.

<servlet-name>  : A name is given to the servlet withing this tag, name could be any user defined value. <servlet-class> : Here we give the name with path to exact servlet class (java file).

<url-pattern>   : Here we gives the url pattern of requested url, on which the servlet will be called.

Lets understand this servlet mapping with an example, lets assume we have an web application with a host name www.yahoo.com, now we want to call a servlet for login on requested url like www.yahoo.com/login.Here /login is the url-pattern for servlet, lets give it a name LoginServlet in servlet-name tags.

Whenever a requested url like www.yahoo.com/login be requested, it will search for an url-pattern with /login in web.xml, now the servlet-name to this mapping will be compared to servlet block and required class file will be called according to servlet-class tag.

So this is how a web container maps a request from a client and invoke a right servlet and right method and the dynamic response is being served to the client.

In this blog we came to know about the architecture of servlet and a basic introduction to web containers. In our upcoming blogs we will come to know more about Servlet Lifecycle and Implementation of doGet() and doPost methods. In upcoming tutorials we will learn more techniques related to servlets and jsp's and try to create our very first hello world web application with jsp and servlet.