tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <Yoav.Shap...@mpi.com>
Subject RE: Why File.toURI() makes ServletContextListener jump contextDestroyed() directly?
Date Tue, 15 Oct 2002 13:03:31 GMT
Hi,

>I found when I use method toURI( ) of class File in
ServletContextListener,
>it will jump contextDestroyed( ) directly.
>Did I misuse?

Yes.

Exceptions in your contextInitialized() method can result in the
container not initializing your context.  In fact, the desired behavior
IMHO in that case is immediate shutdown, which in turn calls
contextDestroyed().  So I think tomcat's doing well in this case.

>public  class  Test  implements  ServletContextListener {
>     public Test( ) {
>          System.out.println("Constructor");
>     }
>     public void contextDestroyed(ServletContextEvent sce) {
>          System.out.println("Destroy");
>     }
>     public void contextInitialized(ServletContextEvent sce) {
>          System.out.println("Init");

Add this here:

try {
  File myFile = new File("C:\\");
  if(myFile != null) {
    URI myUri = myFile.toURI();
    if(myUri != null) {
      System.out.println("Test: contextInitialized(sce): myUri = " +
myUri.toString());
    }
  }
} catch(Exception e) {
  System.err.println("Test: contextInitialized(sce): exception obtaining
myUri: " + e);
}

>          System.out.println("Init  finished");
>     }
>}
>

In general, if you're doing a servlet context listener, you either:

1. Handle every exception, or
2. Be prepared to have immediate shutdown if you have an uncaught
exception.

Yoav Shapira
Millennium ChemInformatics

Mime
View raw message