When is init method of a servlet called




















This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence: The servlet is constructed, then initialized with the init method.

Any calls from clients to the service method are handled. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized. In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.

Author: Various See Also: GenericServlet , HttpServlet Method Summary void destroy Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.

ServletConfig getServletConfig Returns a ServletConfig object, which contains initialization and startup parameters for this servlet. String getServletInfo Returns information about the servlet, such as author, version, and copyright. Method Detail init void init ServletConfig config throws ServletException Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

After the destroy method is called, the servlet object is marked for garbage collection. Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service method of a single instance of the servlet. Servlets - Life Cycle Advertisements. Previous Page. Next Page. Previous Page Print Page.

Save Close. The servlet container cannot place the servlet into service if the init method. But: if u want to call init explicitly than u call as normal function of your class. The question asked in the interview was meant to make your knowledge knowable to them in the following fields:.

Number 4. It's like to construct a building and then to call for the buyers to settle in it and then begin putting its structure. They were asking this to know your managing and organizational talents over project handling. The init method is designed to be called only once. It is called when the servlet is first created, and 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 is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started. When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate.

The init method simply creates or loads some data that will be used throughout the life of the servlet. No i suggest not to call ,because init method should be called only once to make some initializations like Data base connection code ,this has to be called only once ,no reason to call it more than once where as service method will be called for every user request with user specific request and response objects.

Imagine if u make call for init from service method then init will called so many times. So it is not a good idea to call like that. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 7 months ago. Active 6 years, 5 months ago. Viewed 13k times.



0コメント

  • 1000 / 1000