Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
We declare ( <%! ) an int outside the servlet's methods, so it will be an instance variable, as opposed to a local, temporary one.
As long as the servlet instance is alive, this one stays alive, too.
Defining methods is done like this,
also a declaration.
Called in this expression.
Scriptlets, that you've seen previously ( <% ) , always result in source code inside the servlet's service method, so, obviously, you couldn't declare a method there.
Variables declared in scriptlets are always local to the service method.
DON'T use declared variables to store client specific data, one single instance of the servlet, containing the one variable , serves many clients !!!
Use the session object instead. More in the JSP Session lesson.
This line should actually read : This servlet instance's hit counter. For a real page counter you'd store a counter in a file or database, because other instances can serve the same page.