Step 1: A user using a web browser requests some information from the web server via an http request.
Step 2: The web server receives the request. If the request is for a straightforward HTML page then the appropriate HTML file will be loaded. If the request is to a servlet, then the web container will load and initiate the servlet (unless it was already running). This is done by running the servlet on a Java virtual machine (JVM).
Step 3: The servlets’ init() method is then executed. This method is the equivalent of the init() method defined for applets. That is, it is executed only once, when the servlet is first created. It should be used in the same way as the init() method for applets. That is, as the servlets initialisation method (rather than defining a constructor). The init() method must complete before any requests are handled.
Step 4: The servlet will receive the HTTP request and perform some ty pe of process. Each request is handled by its own thread (lightweight Java process). Depending upon the request one of the following methods will be called to handle the request:
- doGet – handles GET, conditional GET and HEAD requests.
- doPost – handles POST requests.
- doPut – handles PUT requests.
- doDelete – handles DELETE requests.
Step 5: The servlet will return a response back to the Web server from one of the above methods.
Step 6: The Web server will forward the response to the client.
Step 7: When requested, the web server will terminate the servlet. This may be done by the web server administrator. At this time the destroy() method is called. This method runs only once and is used to “tidy” up any system resources used by the servlet etc. For the servlet to be run again, it must be reloaded by the web server.