aspose file tools*
The moose likes Servlets and the fly likes ServletContextListener v/s init() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContextListener v/s init() method" Watch "ServletContextListener v/s init() method" New topic
Author

ServletContextListener v/s init() method

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jun 07, 2005
Posts: 118
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

Joined: Jan 07, 1999
Posts: 6920
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?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Aug 18, 2004
Posts: 12
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContextListener v/s init() method