Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple servlet ?  RSS feed

 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand a few things about servlets

their is an init and destory method that is called at the begining and end of the life of a servlet.

But I am confused what exactly determines the life of a servlet?
Is this the application server or is it when it empties cache mechanism.... etc

Because this would effect what I actually put into these methods when coding.

Thanks
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the application server. For all practical purposes you can consider that your servlet will be initialized when the application server starts up (or when it gets the first request for the servlet), and that your servlet will be destroyed when the application server shuts down.

In most cases you won't need to put any code in those methods. Nowadays there are better ways of getting code run when the application starts and stops than attaching code to a particular servlet; the ServletContextListener is one such way.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the server calls these methods. The only guarantee is that init will be called before the first request is routed to the servlet. That'll happen right after web app startup if the servlet is listed as load-on-startup in web.xml, or otherwise when the first request arrives at the server.

It is rare to have anything in the destroy method, by the way.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I gotcha.

So I guess my only question is what is the difference of init method or the servlet constructors.
What would the difference be in putting code in the constructors or init won't the do the same thing or have the same effect?

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a very simple rule, a servlet should not have a constructor, so there's no way to put code into it. :-)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on that a tad, servlet constructors aren't particularly useful because the servlet config has yet to be made available. The init() method is the correct place to handle servlet initialization as it has access to the servlet config and context.

Trying to perform initialization in a servlet constructor is a common trap that novices fall into. Avoid the trap.

And as mentioned previously, avoid the trap of using servlet initialization to perform application initialization. Also a common noob mistake.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!