:-:= Koshy Kerteya :-=:- wrote:
>
> Well, here's a basic question
>
> public class HelloServlet extends HttpServer {
>
> HelloServlet() {
> String alpha = new String("Alpha");
> System.out.println("invoked!!");
>
> }
> //rest of the codes
> }
>
> I thought that I would get "invoked"!!! for each thread (upon each request)
No. The servlet container is required to create only one instance of the
servlet. From the Servlet 2.3 spec:
2.2 Number of Instances
In the default case of a servlet not implementing SingleThreadModel and not
hosted in a distributed environment, the servlet container must use only one
instance of a servlet class per servlet definition.
So you should see your message only one time. After creating the instance, the
servlet container creates a thread for every request and the thread executes
the servlet code, entering through the service() method which calls either
doGet() or doPost().
> I looked up JH's book (1st edition) and couldnt really find a satisfactory
> answer...
Well, the answer is there, but not explicitly stated as such. Look at pages 49
- 51, including the illustration on 51. In the first paragraph of "Instance
Persistence" on pg 49, the book states, "...the server creates a single class
instance. That single instance handles every request..." In other words, the
constructor is only called once.
Also, you don't normally define a constructor. You let the compiler provide a
no-argument constructor, and you provide an init() or init(ServletConfig)
method where initialization code is placed.
You should download the servlet specification from
java.sun.com/products/servlet. This document can answer a lot of questions.
___________________________________________________________________________
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