This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

When ServletContext is initialized  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Buddy,
I would like to know when ServletContext has been initialized?
Thanks.
 
Ranch Hand
Posts: 8934
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the web app gets deployed.

Originally posted by grace_mon:
Hi Buddy,
I would like to know when ServletContext has been initialized?
Thanks.

 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all write a class which implements the ServletContextListener interface and implement its methods.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class MyContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
System.out.println("My Servlet Context Initialised");
}

public void contextDestroyed(ServletContextEvent sce){
System.out.println("My Servlet Context Destroyed");
}
}
Register this class with your web app by adding an entry in the web.xml.
<listener>
<listener-class>MyContextListener</listener-class>
</listener>
When the server starts it will invoke the contextInitialised() method.Thereby you can know whether your web app has been initialised or not.
HTH,
Manjunath
[ May 05, 2002: Message edited by: Manjunath Subramanian ]
 
War War Mon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my listener class, from the contextInitialized method, when I read the attributes from ServetContext, all are still NULL. another question, when ServletContext is initialized?
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I am wrong but wouldn't the ServletContext get initialized when the servlet engine (i.e. Tomcat) got started (as long as there was an entry for the ServletContext's web application)?
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
grace_mon
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at JavaRanch's naming policy. . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A web app is ready for execution and its attributes initialized to the values given in the DD only after the execution of the contextInitialized() method completely.
The web applications (different contexts) are loaded in the order of their entry in server.xml
HTH,
Manjunath
[ May 06, 2002: Message edited by: Manjunath Subramanian ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!