Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() and init(conf)

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When does init() and/or init(conf) get used when you create a servlet? What calls it. Should I be concerned with overriding this method, or should I count on the constructors calling it.
Dale
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
init is called for you by the container. The init method in servlets can be thought of like the constructor in any other class. At least that's how I think of it

So any variable setting or other chores I want to do when my servlet is first loaded, I do in the init method.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dale
The init(Servletconfig conf) method is from the servlet interface, it is called by the web container and passes the config object. You can override it in your servlets but then must remebe to call super.init(ServletConfig).
The init() method is provided in the GenericServlet class and is just there as a convienence to the programmer so you dont have to call super.init(ServletConfig). The init() method (if you override it) is called by the init(Servletconfig) method.
You get that functionality from the GenericServlet class, so if you were trying to make our own servlet hierarchy by just implementing Servlet and ServletConfig you wouldn't get that ability - then again why would you want to do that
hope that helps
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my question is, do you HAVE to call it? When do you feel it is appropriate to call it. Why is it provided?
-Dale
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dale DeMott:
So my question is, do you HAVE to call it? When do you feel it is appropriate to call it. Why is it provided?

The init method is called ONCE, when your servlet is first created -- you do ONE TIME setup things here (its not called for each request) -- people often use it to getInitParameters() defined for the servlet...
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be even more specific:
...do you HAVE to call it?

NO!!
The only time you have to call either of the inits is when you override the init(Servletconfig) then you have to call super.init(ServletConfig).
If you only have init() then you dont have to do anything - your method is called by the init(ServletConfig) that was, in turn, called by the web container.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic