Sunday 1 September 2013

Life cycle request, response?

Life cycle request, response?
Basically servlet state is initialized state. Does not exist state is void state


When the first time request comes from the client.
the constructor called on the servlet, ones the lifetime of a servlet, the constructor() and init() are called on the servlet
and ones the init() is called servlet moves from "Does not exist" to initialized state and ready to be used by other requests
for every subsequent requests that comes in service() method is called and servlet still stays in the initialized state
when the destroy() is called again servlet goes back to from initialized state to does not exist state
All these methods (constructor(),init(), service(), destroy()) are called by the Web Container or Servlet Engine
We can see the servlet Hierarcy below figure



At the top of this hierarchy, Servlet is a interface
GenericServlet implements the Servlet interface and it is a abstract class i.e GenericServlet implements Servlet
if we do not override the init() method, if we dont have any initialization code in your servlet. So when you do not override the init() it takes the default implementation from the Generic
Servlet
Now HttpServlet extends the GenericServlet again it is a abstract class. Here if you do not override the service() method the default implementation from HttpServlet is taken
And in the end 'Your Servlet' this is the servlet which you will code in the servlet class which you actually write should extend the HttpServlet and this is concrete class. And this is the place where you implement the methods that is either doGet() or doPost()



Different Methods of Servlet 

1. init() method:

    init() is called before any request for the servlet can be handled even before any client request can be served the init() is called and init() is called by the Web Container or Servlet Engine.
What init() is for:  to initialize your servlet (it can be data source, to connect your data base or any such initialization you can perform , and 
what do you mean by initialization? it is a one time process that you dont want to initialize everytime every  client request you dont want this particular thing to happen )before handling any client requests. And it happens only once in life time of servlet. 

Now my doubt is we do have a constructor for servlet? when you talk about the java, generally we all put the code for initialization stuff in the constructor of the class right ! then why do we use init() for initialization in servlet? why can't we use initialization in constructor?  my answer is init() has to access special objects (servletContext, servletConfig) and you generally you can have access those objects in your init() during initialization. These two special objects can be accessed in init() method only but in the constructor.So thus the reason we have to use init() to access those objects

Frankly speaking you cant write the constructor in servlet it will take a default constructor implementation given by the jvm.
We know that if we dont add a constructor to your class the JVM will put a default constructor to your class with no implementation that is blank constructor to your class
so container will take the default implementation of the constructor.
We can override the init() method, any kind of initialization code you want to perfom you can override the init() method else you dont override

2. service():

When service() method is called?
Ans: It is called for every request that comes in for particualr servlet. For example when you type the yahoo login servlet, everytime a person makes a request to yahoo login servlet, the service() method of this login servlet is called.

Why do we use service() in servlet?
Asn: to determine which HTTP method(doGet() or doPost()) should be called.

Can we override the service()?
Ans: No, Because the only thing that the service() is to do is to determine the HTTP methods, to write the HTTP method, to invoke your servlet class based on the type of request client has sent. so you dont really need to override it. Whatever functionality you required for the service() method that is already presented in the HTTPServlet.

3. doGet() or doPost() methods

basically these two methods are used to put all your business logic. The type of request was get, we override the doGet() methods. When the type of request was post, then we override the doPost() method.

When doGet() or doPost() methods are called?
Ans: It is called by the service() method based on the type of request and you put your business logic.

Do you override the doGet() and doPost() methods?
Ans: Yes, Because you have writted your servlet to provide some business logic some dynamic logic to be presented to the end user and it is only place you put your business logic.
Basically doGet()you actually can call other classes and other methods for that your business logic is not in single place, write across multiple classes and it is easier to understand.

4. destroy() method?
when the session time is out. The Web Container calls a destroy() method

Web Container supports Multithreading?
The container runs multiple threads process multiple requests to a single servlet



Fig: Multithread:
Lets take a example you login to yahoomail.com, and sending email, user name and password and the yahoomail servlet actually will give you back your inbox all your messages. Lets say client A login through yahoomail and sends username "abc" so What the Web Container does? The Container sponsers Thread A for client A and passed in the request as "abc". Now at the same time client B makes a request to the yahoo servlet and passing the request username as "xyz" to Thread A so now Web Container sponsers another Thread B for the client B and pass the request "xyz" to the Thread B so Thread A process the request "abc" and create a response object and send back to the response for client A. 
Similar fashion Thread B takes this request "xyz" and creates a response object and process the request send back to the response for client B.

One thing you have to note that these two different threads (Thread A and Thread B) are response but the request and response objects are different and they do not share the same request and response objects. So Thread A has its own request and response object and Thread B has its own request and response object. Basically how the multithreading works for a servlet.









No comments:

Post a Comment