• Post Reply Bookmark Topic Watch Topic
  • New Topic

When ServletContext is initialized

 
War War Mon
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
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.
 
Manjunath Subramanian
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?
 
Ricardo Cortes
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)?
 
Paul Stevens
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!