aspose file tools
The moose likes JSP and the fly likes jspInit() / jspDestroy() because my counter get reset Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "jspInit() / jspDestroy() because my counter get reset" Watch "jspInit() / jspDestroy() because my counter get reset" New topic
Author

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

Tom Triolo
Greenhorn

Joined: Oct 06, 2002
Posts: 15
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

Joined: Jan 14, 2003
Posts: 171
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.


Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56230
    
  13

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


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Tom Triolo
Greenhorn

Joined: Oct 06, 2002
Posts: 15
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: jspInit() / jspDestroy() because my counter get reset
 
Similar Threads
Multiple Struts-config.xml in Jboss 4.2.1
jsp-lifecycle
Servlet Query
Life cycle of Jsp
mock exam question?