Initializing Servlets



I've been going through Sun's J2EE tutorial, and I realize that its goal is to
show as many examples as possible, which leads to the following question:

What is the relationship of the ServletContextListener and the
contextInitialized/contextDestroyed events to the servlet's init() and
destroy() methods? Is it a purely 1:1 relationship? I.e. every time a
contextInitialized event gets set to the listener, exactly one call to the
servlet's init() method can be expected?

If so, what are the considerations for using one or the other mechanism to
initialize context-global data?

In my case, I want to open a file that is available for any servlet thread to
write to, and close it when the servlet is destroyed. Can't I do that in the
init() and destroy() methods?


scott
.



Relevant Pages

  • Re: using finalize() to persist objects
    ... > the web server, ... you should be able to serialize in the destroy() method. ... servlet instance and replace it with a new instance of the same servlet ...
    (comp.lang.java.programmer)
  • Re: How to write user Msg in init in Servlets?
    ... > I wonder how to write messages to users(i.e in explorer) the in init ... > public void init() throws ServletException{ ... least) two ways to send a message from a servlet during an init: ...
    (comp.lang.java.programmer)
  • Re: Seems like my servlet isnt overloading the init() method..can someone plz help?
    ... > that the init() method is only called once the first time the servlet ... The application server has almost certainly redirected the standard output ... standard output stream - typically, ...
    (comp.lang.java.programmer)
  • Re: Seems like my servlet isnt overloading the init() method..can someone plz help?
    ... > that the init() method is only called once the first time the servlet ... > public class MyServletA extends HttpServlet { ... Nigel Wade, System Administrator, Space Plasma Physics Group, University of Leicester, Leicester, LE1 7RH, UK ...
    (comp.lang.java.programmer)
  • Re: Sun one WS : clean shutdown
    ... I had a hunch I could use it. ... But how do I know which servlet is 'destroyed' last? ... I can specify in my web.xml which one gets loaded first, ... > I believe that the servlet destroy() method is what you want. ...
    (comp.lang.java.programmer)