Monday, May 6, 2013

Basic information of a servlet and servlet container

What's a Servlet :

A Servlet is a normal java program which provides the implementation of javax.servlet.Servlet interface.

We can also define as a server side program which handles all the requests from the client. For this reason frameworks that follows MVC design pattern will consider a servlet as controller.

Example for servlet: 

Here is a servlet program which is implementing javax.servlet.Servlet interface.

We have created our own servlet SampleServlet by implementing Servlet interface. As we are familiar with interfaces, we need to provide implementation to all abstract methods of interface in the corresponding implementation class.

So we are implementing all the abstract methods( init(), service(), destroy(), getServletConfig() and getServletInfo() ) as part of our SampleServlet.

We need to configure our servlet as part of deployment descriptor( web.xml ) in order to access from the client. 

A servlet is defined with the help of servlet tag whose child tags describes the name of servlet and class.

To use our servlet as part of our application we need to give url pattern to the servlet as mentioned.

What happens when we deploy our project:

When we deploy our project in the server, server will start parsing web.xml with the help of parsers(SAX,DOM) in it. while parsing the xml if it founds any error it will throw exception. If everything fine, then server will check for the servlet class we have defined in the web.xml.

If it doen't found the class in the class path it will through ClassNotFoundException. If it finds the class, then server will loads the class into the jvm memory and creates servlet's ( SampleServlet) object as we have mentione load-on-start up tag.

What is a ServletContainer:

The implementation of servlet api as part of the server will be known as servlet container. Every server need to provide implementation of servlet api to run the servlets as part of the server. A servlet container is a part of web/application server to handle servlets. A servlet's life cycle methods will be called by the servlet container.

Servlet life cycle :

Life cycle describes when the servlet object gets created and when it will be removed by calling appropriate methods.

init(), service() and destroy() are the life cycle methods of a servlet.

When we deploy our project servlet container will creates object to our servlet and immediately it will call init() of our servelt.

init() will be calles only once through out the life cycle of the servlet. So when the object is created what ever the task which we want to perform only once, will do as part of init(). examples are opening a file, opening database connection etc.

Now when ever client sends the request to the server to access our servlet, just like http://ip address:server port/ServletApp/ servlet container will call service() of SampleServlet. 

For every request from the client container will call service() of our servlet. so service() will be called any number of times based on the request from the client.

For every request what ever the task which we want to perform , will be provided as part of service().

Now, when the project is undeployed from the server , servlet container will call the destroy() and removes the servlet object from the jvm memory.

destroy() will be called only once and as part of destroy we will provide code to close connections, files, clean up memory (removing objects) etc.

After destroy() got called, container will not call service() as there is no servlet object.

getServletConfig() will return ServletConfig object by using which we can get the details of servlet like class, name of servlet etc.

getServletInfo() will return  string information like version,author etc. related to the servlet.

Working with GenericServlet and HttpServlet's will be discussed in next article.

1 comment: