• Post Reply Bookmark Topic Watch Topic
  • New Topic

jspInit() / jspDestroy() because my counter get reset

 
Tom Triolo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a Java Tag for a counter in my web pages. I have several pages that use the same tag. This tells me the total of all the hits on the site, regardless of which page was loaded. However, and I think this may be because it is a shared JVM, the counter gets reset quite often. I might try to use a jspInit() method and jspDestroy(). However, here is where I get confused. I don't want to use a database, I would just like to use the ServletContext obj and reference the <context-param> element.
My question is this, do I write to a file and use an expression in the <param-value><%= expression %></param-value> like thus, to access the value? I'm not even sure if this is allowed? Or maybe, I am just making this too complicated.
Any help, or discussion, or light shed on the subject, would be appreciated.
Thanks,
Tom
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,
I think you will need to have a look at using a database or some other form of persistent storage. If you are storing the page counts on the servlet context then they will be automatically reset when the server/JVM is restarted.
The context params are really read-only attributes i.e. you will not find it easy to update these values with an updated hit count during the destroy() method.
The normal solution would be to use a database to store the hit count, why are you averse to using a DB ? Alternatively you could create a custom file-based approach to read/write the hit counts as appropriate but it would be a bit more work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For something as simple as storing a single count, reading and writing a file would be much simpler than setting up a DB just to store a single number.
The only issue would be making sure that access to the file is synchronized properly.
hth,
bear
 
Tom Triolo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I will try to write to a file in the jspDestroy and read in the jspInit. If that doesn't work, then maybe I will try the DB. At first I was averse to using a DB because I didn't want to burn the one that came with my web server plan> But as I thougt about it, it would just be a table and I could still set up other tables in the DB as I needed.
First however, I will try to read and write to a file. It's just one value.
Tom
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!