Hugo Malheiro wrote:
>
> In my servlet i'm not using a constructor, ... Should
> i use a constructor? What should i put it?
>
Servlets have something called a "life cycle". Normal
Java objects have their constructor called, then
eventually get garbage collected. Servlets are a little
more complicated:
1. Servlet is instantiated (new'ed) by your
servlet container. But the servlet container
only knows to use the no-argument constructor.
2. Servlet container calls servlet's init()
method. (There are a couple of different
init methods, so be careful here.) Probably
you want to do constructor-like stuff in
init() rather than in the actual constructor.
3. Requests come in, and the servlet container
calls your servlet's service() method. The
service() method (if you haven't overridden
it) calls your doGet, doPost, etc. (the default
implementation of service() actually does
quite a bit, so be careful if you override it)
4. The servlet container decides to shut down
your servlet. Maybe because the container is
shutting down, maybe because the container is
very busy and needs resources, maybe for some
other reason. The servlet container calls your
servlet's destroy() method so your servlet can
shut down gracefully. (finalize() is eventually
called when the now-defunct servlet is finally
garbage collected, just like a normal object)
5. The servlet container starts your servlet back
up again, go to #1. The cycle can happen many
times over the course of a day. Or maybe it
just happens once. Since there's no way to know
you have to program for the worst case.
Actually, it's a little more complicated than that,
because you've also got the session lifecycle and
servlet context lifecycle. But the above, plus a little
further reading, should be enough to give you an idea
about where constructors fit into things. (They don't,
really)
There are details in the latest version of the servlet
spec, in "The Servlet Interface : Servlet Life Cycle"
section. You can get the latest spec at:
http://java.sun.com/products/servlet/download.html
The spec is actually very readable, even beginners
can get a lot out of it. You might also try the online
Servlet Tutorial at:
http://developer.java.sun.com/developer/onlineTraining/Servlets/Fundamentals/contents.html
there's a section called "The Servlet Lifecycle".
--
Christopher St. John [EMAIL PROTECTED]
DistribuTopia http://www.distributopia.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html