icn
icn

Reputation: 17876

java servlets init and destroy

servlets uses init() to initialize servlets status and destroy to clean up. Is there a special class name we need to put init() and destroy()? How does servlets know where to find these methods?

asp.net has a global.asax to handle similar thing in asp.net , servlets has a special class to do the same thing?

Thanks

Upvotes: 0

Views: 1430

Answers (5)

sasi
sasi

Reputation: 4318

Servlets are deployed in the Container(Web Server/Application Server),That container will take care of initializing or destroying of servlets,and we dont have predefined classes to initialise and destroy for servlets,if we use particular class that means we are depending on that class(Tightly Coupling) thats not recomended. Now we are using GenericServlet,and HttpServlet classes for that methods..once refer J2EE API and find this classes and methods in them..

Upvotes: 1

Basanth Roy
Basanth Roy

Reputation: 6490

It should be in the same class as your Servlet class.

All servlets inherit this method from the base Servlet class. Unless you want to do some additional processing, the inherited method should be fine and you dont need to override this method in each of your servlets.

Upvotes: 1

gpeche
gpeche

Reputation: 22514

Servlets are managed objects. This means that they are executed inside a container that manages their lifecycles (instantiates the servlets, call their relevant methods when it is appropiate, and releases them). The container (Tomcat, Glassfish, ...) knows when to call these methods at the right time because it is implemented that way, there is nothing special about that.

If the container had a bug, it could even call, say, destroy() at init time and init() at destroy time. That bug would be fixed quickly, though.

Upvotes: 0

Lukas Knuth
Lukas Knuth

Reputation: 25755

The JVM which runs the Servlet, looks for those methods only in classes, which extend Servlet or HttpServlet.

Upvotes: 0

dlev
dlev

Reputation: 48596

Servlets will ultimately be a subclass of the javax.servlet.Servlet class, which defines those methods.

Upvotes: 3

Related Questions