tl;dr
- Your Java Servlet may be loaded and unloaded at will by the Servlet Container, per the Servlet Specification.
- Nothing for you to worry about. Just know that an instance of your Servlet will always be loaded and ready when a request arrives. The rest is up to the container.
Specification
The lifecycle of a Servlet is spelled out in the Java Servlet Specification. For Version 4, see JSR 369. This document will be superseded by the Jakarta.ee spec, but that spec is currently blank, with only a cover sheet and license statement.
Loading a servlet
➥ A servlet container may elect to load a servlet before the first request arrives. Or the container may wait until needed when the first request arrives.
Section 2.3 *Servlet Life Cycle, subsection 2.3.1 Loading and Instantiation reads:
The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.
When the servlet engine is started, needed servlet classes must be located by the servlet container. The servlet container loads the servlet class using normal Java class loading facilities. The loading may be from a local file system, a remote file system, or other network services.
After loading the Servlet class, the container instantiates it for use.
Ending a servlet
➥ A servlet container may choose to keep a servlet loaded for any amount of time. The servlet may stay loaded continuously, or may be ended (likely when idle or when low on memory).
If, after ending the servlet, a new request arrives, the servlet container loads the servlet again. No problem.
To quote the spec, section 2.3.4 End of Service:
The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between.
When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down.
Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.
Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet’s class.
After the destroy method completes, the servlet container must release the servlet instance so that it is eligible for garbage collection.
Don’t worry, be happy
As for your points:
After a servlet handles a request, will it be garbage collected immediately? Or does it stay alive for some more time expecting some request that may follow?
If a servlet dies immediately, it's a pity for the the requests that follow.
If a servlet lives for long, it could be a waste of resources.
…this is the business of the Servlet Container. Not for you to worry about. All you need to know as a Servlet programmer is that an instance of your Servlet will be loaded and ready when a user’s request arrives. The rest is up to the container to manage.
The entire point to inventing the Java Servlet spec was to free you from such concerns. Letting the container run the show is what makes writing a Java Servlet relatively simple, is what puts the “let” in “servlet”. You are not writing a full-blown server, just a small piece of functionality that fits into a larger already-running server.
You asked: Is there any way to decide/configure how long a servlet should live after it is in idle state?
The Servlet standard does not specify any such options, though each servlet container implementation is free to provide their own features beyond those required by the standard. Study the documentation for your particular servlet container.