Documente Academic
Documente Profesional
Documente Cultură
Servlets are normal Java classes which are created when needed and destroyed when not needed. Since
Servlets run within a Servlet Container, creation and destruction of Servlets is the duty of Servlet Container and
not yours. Implementing the init() and destory() methods of Servlet interface allows you to be told by the Servlet
Container that when it has created an instance of your Servlet and when it has destroyed that instance. An
important point to remember is that your Servlet is not created and destroyed for every request it receives,
rather it is created and kept in memory where requests are forwarded to it and your Servlet then generates
response.
Servlet Initialization
So you have created your Servlet class by extending the HttpServlet class and have placed it in /WEB-
INF/classes/ directory of your application. Now when will Servlet Container create an instance of your Servlet?
there are two situations :
When you have specifically told the Servlet Container to preload your Servlet when the Servlet Container starts
by setting the load-on-startup tag to a non-zero value e.g.
• <web-app>
•
• <servlet>
• <servlet-name>test</servlet-name>
• <servlet-class>com.stardeveloper.servlets.TestServlet</servlet-class>
• <load-on-startup>1</load-on-startup>
• </servlet>
•
</web-app>
So when the Servlet Container starts it will preload your Servlet in the memory.
If your Servlet is not already loaded, it's instance will be created as soon as a request is received for it by the
Servlet Container.
During the loading of the Servlet into the memory, Servlet Container will call your Servlet's init() method. Since
init() is going to be called only once you can put Servlet initialization code in it like getting hold of a database
connection or whatever.
Responding to Requests
Once your Servlet is initialized and it's init() method called, any request that the Servlet Container receives will
be forwarded to your Servlet's service() method. HttpServlet class breakes this service() method into more
useful doGet(), doPost(), doDelete(), doOptions(), doPut() and doTrace() methods depending on the type of
HTTP request it receives. So in order to generate respose you should override the doGet() or doPost() method
as required.
At this moment all the requests will be forwarded to the appropriate doGet() or doPost() or whatever method as
required. No new instance will be created for your Servlet.
Servlet Destruction
When your application is stopped or Servlet Container shuts down, your Servlet's destroy() method will be
called. This allows you to free any resources you may have got hold of in your Servlet's init() method.
Always Remember
init() and destroy() methods will be called only once during the life time of your Servlet while service() and it's
broken down methods ( doGet(), doPost() etc ) will be called as many times as requests are received for them
by the Servlet Container.
We have now finished learning about Servlet life cycle and we now know about init() , service() and destroy()
methods. We can now move on to learn more about HttpServlet class which almost all of your Servlets will
extend.
• The service phase of the Servlet life cycle represents all interactions with requests until the Servlet is
destroyed. The Servlet interface matches the service phase of the Servlet life cycle to the service()
method. The service() method of a Servlet is invoked once per a request and is responsible for
generating the response to that request. The Servlet specification defines the service() method to take
two parameters: a javax.servlet.ServletRequest and a javax.servlet.ServletResponse object. These two
objects represent a client's request for the dynamic resource and the Servlet's response to the client. By
default a Servlet is multi-threaded, meaning that typically only one instance of a Servlet1 is loaded by a
JSP container at any given time. Initialization is done once, and each request after that is handled
concurrently2 by threads executing the Servlet's service() method.
• The destruction phase of the Servlet life cycle represents when a Servlet is being removed from use by
a container. The Servlet interface defines the destroy() method to correspond to the destruction life
cycle phase. Each time a Servlet is about to be removed from use, a container calls the destroy()
method, allowing the Servlet to gracefully terminate and tidy up any resources it might have created. By
proper use of the initialization, service, and destruction phases of the Servlet life cycle, a Servlet can
efficiently manage application resources. During initialization a Servlet loads everything it needs to use
for servicing requests. The resources are then readily used during the service phase and can then be
cleaned up in the destruction phase.