I know that for each request to a servlet a the doPost() or doGet() methods are executed and that the code wirtten inside init() method is initialised only once. But what about the code written outside all these methods?
Is that code also threaded? I mean varibles declared in that part, if they are modified in the doPost(), will those changes be reflected for other requsts to the servlets?
I know that for each request to a servlet a the doPost() or doGet()
Share
In a normal servlet container, there is only one instance of the servlet object. This object may be used by any number of Threads – one Thread per request. Managing the lifetime of a servlet instance is up to the servlet container.
Hence, when changing the value of a class variable in any method (including init()), it will affect all subsequent requests.
Changing or declaring a local variable within your method of course does not influence anything, as the next time the method is called, the local variable is created again (and gets destroyed by the garbage collector when the method is finished).