This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access counter for servlets

 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • 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
 
Cole Terry
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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.
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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: 157
  • Mark post as helpful
  • send pies
  • 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 ??
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic