In general you can store the count value whenever you want, file, database of any kind and so on.
However I would like to mention that servlets in general are designed to run in environments where many users can access the servlet simultaneously, so you always have to think about the synchronization.
Multiple instances of the serlvet class can exist (it's up to you web container when to create them).
In general what you need to do is:
- Get the current value
- Increase the value by 1
- Store the value
Now this thing has to be carefully implemented.
Depending on the underlying layer you might want to consider using Transactions:For example for postgesql
Another example is using a NoSQL store like Redis:
Read Pattern counter section
The best way to implement a distributed counter really depends on the underlying storage so its hard to advice on anything more specific, the choice is yours.
Another aspect I would like to mention is a distributed nature of web application.
If its a real world application, eventually you might want to run it on multiple service and 'scale it out' (at least its a trend in a modern server side software in my opinion).
From this perspective, the application can't really use local file and, say, windows registry because the file system is not a distributes thing (unless you use a distributed file system).
Hope, this helps