Professional Documents
Culture Documents
Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four
stages:
1) Loading a Servlet.
2) Initializing the Servlet.
3) Request handling.
4) Destroying the Servlet.
1) Loading a Servlet: The first stage of the Servlet lifecycle involves loading
and initializing the Servlet by the Servlet container. The Web container or
Servlet Container can load the Servlet at either of the following two stages:
1) Loading : Loads the Servlet class.
2) Instantiation : Creates an instance of the Servlet.
2) Initializing a Servlet: After the Servlet is instantiated successfully, the
Servlet container initializes the instantiated Servlet object. The container
initializes the Servlet object by invoking the Servlet.init(ServletConfig)
method which accepts ServletConfig object reference as parameter.
3) Handling request: After initialization, the Servlet instance is ready to serve
the client requests.
4) After the destroy() method is executed, the Servlet container releases all the
references of this Servlet instance so that it becomes eligible for garbage
collection.
3) What is cookies in servlet explain with example?
Ans:
1) Cookie is a small piece of information that is persisted between the
multiple client requests.
2) It is Stored in key-value pair format to the client’s browser during multiple
requests.
3) When the client generates a request, the server gives the response with
cookies having an id which are then stored in the client’s browser.
4) To make a cookie, create an object of Cookie class and pass a name and its
value.
5) To fetch the cookie, getCookies() method of Request Interface is used.