• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access counter for servlets

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in the code I am having, does this gives a holistic counter of number of times a servlet is accessed. Will the counter variable retain the value even after the application is undeployed ??

If not please anyone tell me how to get the counter variable retain the value of number of times servlet is accessed.

Here is my code




Thanks
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shivang sarawagi wrote:I have a doubt in the code I am having, does this gives a holistic counter of number of times a servlet is accessed. Will the counter variable retain the value even after the application is undeployed ??



Absolutely no, unless you persist the variable's value into a file or database.

shivang sarawagi wrote:
If not please anyone tell me how to get the counter variable retain the value of number of times servlet is accessed.



It always retains its value during servlet's life because it is instance variable and there is only one instance of the servlet per web app.
 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other thing, which you can use if you really want to keep a count of active sessions at any point of time during the lifecycle of your web app is to add the counter increment/decrement logic in HttpSessionListener..

Is this what you are looking for?

And the values would not be persisted, if the application crashes or the web app is brought down. You need to explicitly persist it.
 
shivang sarawagi
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply guys Can anyone tell me how to explicitly persist the value of the counter variable ??

The information which is given at the bottom of webpages about the number of times the page is excessed, in that is the variable explicitly persisted ??
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone tell me how to explicitly persist the value of the counter variable ??



Two ways to persist the Counter value that I know

1. Save counter value to a flat file on disk

OR

2. Save counter value to a Database table


Assuming
a.you have Servlet/JSP application running on any Web container
b. you want to persist value of Counter

Your alogrithm can be

Step 1 -> Treat your counter as Application/ServletContext level variable (rather than any servlet class variable).

Step 2 ->

Implement ServletContextListener interface and override its two methods


Step 3 -> After creating the listener class, you register same in web.xml file as listener.
Sample code is shown below (CustomServletContextListener is the class which implements ServletContextListener )



Step 4 -> When ever you want to increase Counter value, just get current value from Servlet Context and , increase that value and set it back in Servlet Context. In this way, counter will always show current value on your JSP.


 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic