freelanceprogrammers.org Forum Index » Java

What`s servlet?


View user's profile Post To page top
estrose Posted: Mon Feb 18, 2002 4:20 pm


Joined: 18 Feb 2002

Posts: 6
What`s servlet?
I declared a servlet class with doGet()
inside:<br><br>class CounterServlet<br> int intCount;<br>
doGet(){intCount++;}<br><br>}<br><br>Variable intCount is not static, but it
acts like static,
why?<br>Is it true that every servlet has it`s one doGet
method, but other pieces of code like class-level
variables and methods are shared between all clients?
Please help me out. <br>Thx
Reply with quote
Send private message
View user's profile Post To page top
mcconnell_stephen Posted: Tue Feb 19, 2002 12:38 am


Joined: 01 Feb 2002

Posts: 73
What`s servlet?
Only one instance of the servlet is created in
the JVM. However, each time you call the doGet or
doPost method on a servlet, a new thread of that method
is created.<br><br>Therefore, the class variable
inCount only is created once, but each time you call the
doGet/doPost (and any other person who calls it through a
browser) will increment the single instance of
intCount.<br><br>You need to keep your doPost/doGet methods
threadsafe. Unless you want the above behavior (a counter for
people visiting the page or something like that), you
need to make all your variables method level
scope.<br><br>What you want the servlet to do each time you call it,
should be contained in the doGet/doPost method anyway.
Access variables declared in those methods.<br><br>Steve
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help