• Post Reply Bookmark Topic Watch Topic
  • New Topic

Startup servlet getting initialised twice.

 
Jaydeep Deshmukh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I am experiencing a very strange behaviour with the startup servlet. I have a startup servlet defined in my webapp's web.xml file. When I checked the logs, the file gets initialised, destroyed and gets initialised again. Here is the entry from my web.xml

<servlet>
<servlet-name>init</servlet-name>
<servlet-class>com.msdw.aps.taxportal.util.common.TPInitServlet</servlet-class>
<init-param>
<param-name>CONFIG_MODE</param-name>
<param-value>INTEG</param-value>
</init-param>
<init-param>
<param-name>INTEG_DSN_FILE_TAXPORTAL</param-name>
<param-value>/ms/user/d/deshjay/aps/taxportal/0.1/src/WebRoot/xml/xyz.dev.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Any clues?

Thanks
Jaydeep
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see any exceptions in the logs?

- Susanta
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you are using Servlets 2.3 or later, why are you using a "startup servlet" versus a context listener?
 
Jaydeep Deshmukh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Susanta,
I did not see any exceptions in the log. the logs that I see are startup servlet's init method called...then destroy method and again init()

Bear,
Never used a contextListener before. Can you put more light on it?

Thanks
Jaydeep
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're very simple.
The API sums it up pretty quickly:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html

The nice thing about them is that the contextInitialized method gets called once and only once when the app loads.
Also, no servlets are loaded until the contextInitialized method is done.

A much better way to inialize an app than by using servlets.

According to the Servlet spec, there is no guarantee that a servlet will stay loaded or only be loaded once during the lifetime of the app. (There can only be one instance but that instance can be loaded and unloaded multiple times)


SRV.2.3.4 End of Service
The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between. When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down. Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.


This makes using a servlet (with load-on-startup) a bit risky for application initialization.

ContextListeners let servlets do what they were intended to do; listen for and respond to requests.
[ September 20, 2006: Message edited by: Ben Souther ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!