- where exactly does the servlet get loaded - into the memory, or into the container ??
As per my understanding container loads the servlets into memory.
- also what are the methods that get called on the servlet. init method gets called for sure, what about the service method ? does loading a servlet mean that the servlet gets serviced as well ?
Source: The init, service, and destroy methods are the servlet's lifecycle methods. The init method is called once by the servlet container after the servlet class has been instantiated to indicate to the servlet that it being placed into service. The init method must complete successfully before the servlet can receive any requests. A servlet programmer can override this method to write initialization code that needs to run only once, such as loading a database driver, initializing values, and so on. In other cases, this method is normally left blank.
The service method is then called by the servlet container to allow the servlet to respond to a request. The servlet container passes a javax.servlet.ServletRequest object and a javax.servlet.ServletResponse object. The ServletRequest object contains the client's HTTP request information and the ServletResponse encapsulates the servlet's response. These two objects enable you to write custom code that determines how the servlet services the client request.
The servlet container calls the destroy method before removing a servlet instance from service. This normally happens when the servlet container is shut down or when the servlet container needs some free memory. This method is called only after all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls destroy, it will not call the service method again on this servlet. The destroy method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, and threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.
- also regarding the the load on start up tag in web.xml. Can we have two servlets with load on startup value as 1 ?
Yes, <load-on-startup> tag specifies that the servlet should be loaded automatically when the web application is started. You can define the value in this tag. So multiple servlets can have this tag.