servlet file access question

I want to write a servlet to save the access counts , I tried to save the counts in a file when

the session is created, But I wonder if the more than two session access the file at the same time,

the counts saved in the file should be not correct . How do java treate this?

[307 byte] By [koeicjm] at [2007-9-26 6:40:35]
# 1

Hi

To count the number of unique accesses to you page you can have a static variable in your servlet class which would store this count. The way to increment it would be is to increment it only when creating a new session. This way duplicate accesses from the same client would be ignored.

You could store this count in a file. When the servlet starts up i.e the init() method can load this from the file into the Static variable. And this variable can be written back to disk when the servlet is destroyed i.e in the destroy() method. This way all manipulation is performed in memory.

Keep me posted on your progress

Good Luck!

Eshwar Rao

Developer Technical Support

Sun microsystems Inc.

http://www.sun.com/developers/support

Eshwar_indts at 2007-7-1 15:58:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...