variables delcared using %! %> are not local to service method. They will be class variables.So x is initialized only once when the instance of servlet is created. Separate thread is created for each request and all requests share the same variable x.
SCJP 5.0, SCWCD 5, preparing for SCDJWS
Joined: Apr 21, 2008
Hi Urs. During initialization, the container calls the init() method and it calls jspInit(), here it prints 'x' value to '0' and increments it to '1'(since it is post increment). In 'x' value becomes '1', and increments it becomes '2'. Now the 'x' value is '2' and the container calls the and outputs '2'. After that it calls jspInit(), it prints '2' and incremets and 'x' becomes 3.
In second request, container calls service() method only. In _jspService() method, the jsp expression value is '3' and incremets 'x' and it becomes '4'. After that it calls System.out.println(x) and prints '4'. And calls jspInit() method( its like calling inline method) and prints 4 and increments 'x' and it becomes '5'.