Servlets typically have the operating cycle, as shown in Figure 26-5.
Figure 26-5. Operating cycle of a servlet
The sequence of events in a servlet's lifetime is:
The servlet container starts up, and at some point constructs an instance of the servlet and calls its
init() method. The
init() method is only called once. Not once per request, not once per session, but once at the beginning of the servlet's lifetime. The
init() method is a good place to put code to open a database connection.
Unlike, say, a GUI, there is no background thread always running code in the servlet. ...