Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
34
Chapter 2 First Servlets
Figure 2 4
Result of the HelloWWW3 servlet.
2.6 The Servlet Life Cycle
Earlier in this book, I vaguely referred to the fact that only a single instance
of a servlet gets created, with each user request resulting in a new thread that
is handed off to 
doGet
 or 
doPost
 as appropriate. I'll now be more specific
about how servlets are created and destroyed, and how and when the various
methods are invoked. I'll give a quick summary here, then elaborate in the
following subsections. 
When the servlet is first created, its 
init
 method is invoked, so that is
where you put one time setup code. After this, each user request results in a
thread that calls the 
service
 method of the previously created instance.
Multiple concurrent requests normally result in multiple threads calling 
ser 
vice
 simultaneously, although your servlet can implement a special interface
that stipulates that only a single thread is permitted to run at any one time.
The 
service
 method then calls 
doGet
, 
doPost
, or another 
doXxx
 method,
depending on the type of HTTP request it received. Finally, when the server
decides to unload a servlet, it first calls the servlet's 
destroy
 method.
The init Method
The 
init
 method is called when the servlet is first created and is not called
again for each user request. So, it is used for one time initializations, just as
with the 
init
 method of applets. The servlet can be created when a user first
invokes a URL corresponding to the servlet or when the server is first started,
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved