Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContextListener v/s init() method

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a piece of code that should execute in the beginning (initialization code), where should I put that code? in ServletContextListener or in init() method?

Thanks.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not sure which servlet will be called first then put that code in ServletContextListener, otherwise in init() method...

Is this the right answer and right reasoning?
 
geeta lalchandani
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a servlet you will load on start-up, you can out it in the
init(). If it can be other servlet then put in the context listener

There is a conception, that we should avoid putting lots of code in the init().
Java Ranchers please comment on this.
According to me,init method is meant to initialise. If the initialisation code becomes bulky, it can be split in other methods and call those methods in init.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is really one of when things (can/might) get executed.

A ServletContext Listener will be invoked as soon as possible after the servlet context is created. This is almost always when the server starts up, although it can also be when a whole servlet container or web application is reinitialised for some reason.

The "init" method of a servlet, on the other hand, is only guaranteed to be called after a particular servlet has been loaded but before it serves any requests.

Servlet containers are free to "init" all servlets at the start, defer running "init" until the first request comes in for that servlet, or any time between these two events.

So. If you definately need your code to execute when the container starts, you should use a servlet lifecycle listener. If you just need to make sure your code has already been executed before a request has been processed, but don't really care when it is done otherwise, put your code in "init".

Has that helped?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the initialization code is specific to that and only that servlet, put it in the init method of that servlet.
If, on the other hand the initialization this code performs is for multiple servlets or is needed by the app as a whole, put it in a context listener.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Carver:
If you definately need your code to execute when the container starts, you should use a servlet lifecycle listener. If you just need to make sure your code has already been executed before a request has been processed, but don't really care when it is done otherwise, put your code in "init".


Thanks Frank.


What difference it makes, executing a code when a container starts or executing a code before the first request...

Hope it is cleat what I want to say...

Thanks.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
If the initialization code is specific to that and only that servlet, put it in the init method of that servlet.
If, on the other hand the initialization this code performs is for multiple servlets or is needed by the app as a whole, put it in a context listener.


If we see MVC application, there is only one servlet that exist (hope I am right here). All other parts are simple java classes and JSPs. So in that case, where it should go???

Thanks.

 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
If we see MVC application, there is only one servlet that exist (hope I am right here). All other parts are simple java classes and JSPs. So in that case, where it should go???


Anywhere you like. You can toss for this.

Note: In MVC you can have as many servlets as you need. You are probably talking about Front Controller pattern.
[ November 30, 2005: Message edited by: Adeel Ansari ]
 
Naslai Shobha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One important thing to note here is the order in which the webserver loads them and secondly what the application demands.

Suppose the logic which you are planning to write in that init() method is something that only one servlet needs, then its better to go for servlet init method.

Whereas if the logic is required by other servlets then it should be in the Servlet Context:

According to the Servlet Spec the following is the order teh server should follow:
Instantiate an instance of each event listener identified by a <listener> element
in the deployment descriptor.
� For instantiated listener instances that implement ServletContextListener,
call the contextInitialized() method.
� Instantiate an instance of each filter identified by a <filter> element in the deployment
descriptor and call each filter instance�s init() method.
� Instantiate an instance of each servlet identified by a <servlet> element that
includes a <load-on-startup> element in the order defined by the load-onstartup
element values, and call each servlet instance�s init() method.

Thanks
Shobha Naslai
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:


If we see MVC application, there is only one servlet that exist (hope I am right here). All other parts are simple java classes and JSPs. So in that case, where it should go???

Thanks.



If this servlet is the only component that depends on this initialization code, yes. If any other components (be they servlets, filters, JSPs, etc.) depend on this initialization, then it should be done with contextListener.

And, as Adeel correctly noted.
The front controller or command pattern should not be confused with MVC.
They often work together but aren't the same thing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic