You guys are confusing me...

At 10:52 27/03/2002 +0100, you wrote:
> Great, that's what I thought.  But here's why I'm getting confused. The
> servlet tutorial says that a servlet is created once and once
> only (that's
> when the init() is run).
> http://java.sun.com/docs/books/tutorial/servlets/lifecycle/index.html
> If the servlet class is created only once, how does Tomcat then create
> multiple instances of the class ?

>In that case, I think Sun might be right ;-) As I said, I have never done
>servlets, the above is just what I suspected...

Yes, this had alarm bells ringing for me - I'm pretty sure the tutorial is
correct. As I understand it, each user request is serviced by a thread and
these threads will all access the *same* instance of the class - this is
precisely why we have to be careful of threading issues and use synchronised
code blocks. From the tutorial:

"HTTP servlets are typically capable of serving multiple clients concurrently.
If the methods in your servlet that do work for clients access a shared
resource, then you can handle the concurrency by creating a servlet that
handles only one client request at a time. (You could also synchronize access
to the resource, a topic that is covered in this tutorial's lesson on threads.)


To have your servlet handle only one client at a time, have your servlet
implement the SingleThreadModelinterface in addition to extending the
HttpServlet class. "

Various scoping mechanisms, such as those pointed out for beans, are available
through various user, request, session and application data structures - see my
book suggestions below.

Eric wrote:

>If I comment out the init() method in my servlet do I get an instance of it
>for each request?
>
>Do people commonly do this?

I suspect that if you do this Eric, it won't compile...

If you'd prefer books written in English, I'd suggest you could do a lot worse
than Hunter & Crawford's Java Servlet Programming (2nd ed) from O'Reilly. As
usual for O'R, its about as good a text as you'll get for the big picture and
covers thread basics.

More 'heavy duty' concurrency stuff (though not specifically related to
servlets), based upon his book 'Concurrent Programming - the Java Programming
Language' is available from Steven Hartley's site:

http://www.mcs.drexel.edu/~shartley/ConcProgJava/

HTH
cam

__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards�
http://movies.yahoo.com/

--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to